ToB企服应用市场:ToB评测及商务社交产业平台

标题: 搭建简单JavaWeb项目 [打印本页]

作者: 何小豆儿在此    时间: 2022-8-20 12:34
标题: 搭建简单JavaWeb项目
参考:(17条消息) 手把手搭建一个完整的javaweb项目(适合新手)_心歌技术的博客-CSDN博客_javaweb项目完整案例
补充项目结构的细节,进行了一点修改,修改为学生信息管理系统
以下是搭建过程:
1.项目结构

 
2.数据库结构

 
 
3.代码部分
 com.dao.StuDao.java
  1. package com.dao;
  2. import java.util.List;
  3. import com.entity.Stu;
  4. public interface StuDao {
  5.     public boolean login(String id,String pwd);
  6.     public boolean register(Stu stu);
  7.     public List<Stu> getStuAll();
  8.     public boolean delete(String id);     
  9.     public boolean update(String id,String name,String sex,String date,String pwd);
  10. }
复制代码
com.dao.StuDaoImpl.java
  1. package com.dao;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import com.entity.Stu;
  7. import com.util.DBconn;
  8. public class StuDaoImpl implements StuDao{
  9.    
  10.     public boolean register(Stu stu) {
  11.         boolean flag = false;
  12.         DBconn.init();
  13.         int i = DBconn.addUpdDel("INSERT INTO stuinfo(id,name,sex,date,pwd)" + "values('"+stu.getId()+"','"+stu.getName()+"','" +stu.getSex()+"','"+ stu.getDate()+"','"+stu.getPwd()+"')");
  14.         if(i>0) {
  15.             flag = true;
  16.         }
  17.         DBconn.closeConn();
  18.         return flag;
  19.     }
  20.    
  21.    
  22.     public boolean login(String id,String pwd) {
  23.         boolean flag = false;
  24.         try {
  25.             DBconn.init();
  26.             ResultSet rs = DBconn.selectSql("SELECT * FROM stuinfo WHERE id='"+id+"'and pwd='"+pwd+"'");
  27.             while(rs.next()) {
  28.                 if(rs.getString("id").equals(id) && rs.getString("pwd").equals(pwd)) {
  29.                     flag = true;
  30.                 }
  31.                
  32.             }
  33.             DBconn.closeConn();
  34.         }catch(SQLException e) {
  35.             e.printStackTrace();
  36.         }
  37.         return flag;
  38.     }
  39.    
  40.     public List<Stu> getStuAll(){
  41.         List<Stu> list = new ArrayList<Stu>();
  42.         try {
  43.             DBconn.init();
  44.             ResultSet rs = DBconn.selectSql("SELECT * FROM stuinfo");
  45.             while(rs.next()) {
  46.                 Stu stu = new Stu();
  47.                 stu.setId(rs.getString("id"));
  48.                 stu.setName(rs.getString("name"));
  49.                 stu.setSex(rs.getString("sex"));
  50.                 stu.setDate(rs.getString("date"));
  51.                 stu.setPwd(rs.getString("pwd"));
  52.                 list.add(stu);
  53.             }
  54.             DBconn.closeConn();
  55.             return list;
  56.         }catch(SQLException e) {
  57.             e.printStackTrace();
  58.         }
  59.         return null;
  60.     }
  61.    
  62.     public boolean update(String id,String name,String sex,String date,String pwd) {
  63.         boolean flag = false;
  64.         DBconn.init();
  65.         String sql = "UPDATE stuinfo set name ="
  66.                 +
  67.                 " '"+name+"',sex ='"+sex
  68.                         +"',date ='"+date
  69.                         +"',pwd ='"+pwd+"'WHERE id ="+id;
  70.         int i = DBconn.addUpdDel(sql);
  71.         if(i>0) {
  72.             flag=true;
  73.         }
  74.         DBconn.closeConn();
  75.         return flag;
  76.     }
  77.    
  78.     public boolean delete(String id) {
  79.         boolean flag = false;
  80.         DBconn.init();
  81.         String sql = "DELETE FROM stuinfo WHERE id ="+id;
  82.         int i = DBconn.addUpdDel(sql);
  83.         if(i>0){
  84.             flag = true;
  85.         }
  86.         DBconn.closeConn();
  87.         return flag;
  88.     }
  89. }
复制代码
com.entity.Stu.java
  1. package com.entity;
  2. public class Stu {
  3.     private String id;
  4.     private String name;
  5.     private String sex;
  6.     private String date;
  7.     private String pwd;
  8.    
  9.     public String getId() {
  10.         return id;
  11.     }
  12.     public void setId(String id) {
  13.         this.id = id;
  14.     }
  15.     public String getName() {
  16.         return name;
  17.     }
  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21.     public String getSex() {
  22.         return sex;
  23.     }
  24.     public void setSex(String sex) {
  25.         this.sex = sex;
  26.     }
  27.     public String getDate() {
  28.         return date;
  29.     }
  30.     public void setDate(String date) {
  31.         this.date = date;
  32.     }
  33.     public String getPwd() {
  34.         return pwd;
  35.     }
  36.     public void setPwd(String pwd) {
  37.         this.pwd = pwd;
  38.     }
  39.    
  40.    
  41. }
复制代码
com.filter.EncodingFilter.java
  1. package com.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. public class EncodingFilter implements Filter{
  10.     public EncodingFilter(){
  11.         System.out.println("过滤器构造");
  12.     }
  13.     public void destroy() {
  14.         System.out.println("过滤器销毁");
  15.     }
  16.     public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
  17.         request.setCharacterEncoding("utf-8"); //将编码改为utf-8
  18.         response.setContentType("text/html;charset=utf-8");
  19.         chain.doFilter(request, response);
  20.     }
  21.     public void init(FilterConfig arg0) throws ServletException {
  22.         System.out.println("过滤器初始化");
  23.     }
  24. }
复制代码
com.servket.DeleteServlet.java
  1. package com.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.ServletResponse;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.dao.StuDao;
  11. import com.dao.StuDaoImpl;
  12. public class DeleteServlet extends HttpServlet {
  13.     @Override
  14.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  15.             throws ServletException, IOException {
  16.         doPost(request, response);
  17.     }
  18.     @Override
  19.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  20.             throws ServletException, IOException {
  21.         
  22.         String id = request.getParameter("id");
  23.         
  24.         StuDao ud = new StuDaoImpl();
  25.         
  26.         if(ud.delete(id)){
  27.             request.setAttribute("xiaoxi", "删除成功");
  28.             request.getRequestDispatcher("/Searchall").forward(request, response);
  29.         }else{
  30.             response.sendRedirect("index.jsp");
  31.         }
  32.     }
  33.    
  34. }
复制代码
com.servlet.DengluServlet.java
  1. package com.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.ServletResponse;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.dao.StuDao;
  11. import com.dao.StuDaoImpl;
  12. public class DengluServlet extends HttpServlet {  //需要继承HttpServlet  并重写doGet  doPost方法
  13.    
  14.     @Override
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  16.             throws ServletException, IOException {
  17.         doPost(request, response);  //将信息使用doPost方法执行   对应jsp页面中的form表单中的method
  18.     }
  19.     @Override
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  21.             throws ServletException, IOException {
  22.         
  23.         String id = request.getParameter("id"); //得到jsp页面传过来的参数
  24.         String pwd = request.getParameter("pwd");
  25.         
  26.         
  27.         StuDao ud = new StuDaoImpl();
  28.         
  29.         if(ud.login(id, pwd)){
  30.             request.setAttribute("xiaoxi", "欢迎用户"+id); //向request域中放置信息
  31.             request.getRequestDispatcher("/success.jsp").forward(request, response);//转发到成功页面
  32.         }else{
  33.             response.sendRedirect("index.jsp"); //重定向到首页
  34.         }
  35.     }
  36.    
  37. }
复制代码
com.servlet.Searchall.java
  1. package com.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.List;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.dao.StuDao;
  12. import com.dao.StuDaoImpl;
  13. import com.entity.Stu;
  14. public class Searchall extends HttpServlet {
  15.     @Override
  16.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  17.             throws ServletException, IOException {
  18.         doPost(request, response);
  19.     }
  20.     @Override
  21.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  22.             throws ServletException, IOException {
  23.         
  24.         StuDao ud = new StuDaoImpl();
  25.         List<Stu> stuAll = ud.getStuAll();
  26.         request.setAttribute("stuAll", stuAll);
  27.         request.getRequestDispatcher("/showall.jsp").forward(request, response);
  28.     }
  29.    
  30. }
复制代码
 
com.servlet.UpdateServlet.java
  1. package com.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.ServletResponse;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.dao.StuDao;
  11. import com.dao.StuDaoImpl;
  12. import com.entity.Stu;
  13. public class UpdateServlet extends HttpServlet {
  14.     @Override
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  16.             throws ServletException, IOException {
  17.         doPost(request, response);
  18.     }
  19.     @Override
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  21.             throws ServletException, IOException {
  22.         
  23.         String id = request.getParameter("id");
  24.         String name = request.getParameter("name");
  25.         String sex = request.getParameter("sex");
  26.         String date = request.getParameter("date");
  27.         String pwd = request.getParameter("pwd");
  28.         
  29.         System.out.println("------------------------------------"+id);
  30.         
  31.         StuDao ud = new StuDaoImpl();
  32.         
  33.         if(ud.update(id, name, sex, date, pwd)){
  34.             request.setAttribute("xiaoxi", "更新成功");
  35.             request.getRequestDispatcher("/Searchall").forward(request, response);
  36.         }else{
  37.             response.sendRedirect("index.jsp");
  38.         }
  39.     }
  40.    
  41.    
  42. }
复制代码
 
com.servlet.ZhuceServlet.java
  1. package com.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.ServletResponse;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.dao.StuDao;
  11. import com.dao.StuDaoImpl;
  12. import com.entity.Stu;
  13. public class ZhuceServlet extends HttpServlet {
  14.     @Override
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  16.             throws ServletException, IOException {
  17.         doPost(request, response);
  18.     }
  19.     @Override
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  21.             throws ServletException, IOException {
  22.         
  23.         String id = request.getParameter("id");
  24.         String name = request.getParameter("name"); //获取jsp页面传过来的参数
  25.         String sex = request.getParameter("sex");
  26.         String date = request.getParameter("date");
  27.         String pwd = request.getParameter("pwd");
  28.         
  29.         
  30.         
  31.         
  32.         Stu stu = new Stu(); //实例化一个对象,组装属性
  33.         stu.setId(id);
  34.         stu.setName(name);
  35.         stu.setSex(sex);
  36.         stu.setDate(date);
  37.         stu.setPwd(pwd);
  38.         StuDao ud = new StuDaoImpl();
  39.         
  40.         
  41.         if(ud.register(stu)){
  42.             request.setAttribute("username", name);  //向request域中放置参数
  43.             //request.setAttribute("xiaoxi", "注册成功");
  44.             request.getRequestDispatcher("/denglu.jsp").forward(request, response);  //转发到登录页面
  45.         }else{
  46.             
  47.             response.sendRedirect("index.jsp");//重定向到首页
  48.         }
  49.     }
  50.    
  51. }
复制代码
 
com.util.DBconn.java
  1. package com.util;
  2. import java.sql.*;
  3. public class DBconn {
  4.     static String url = "jdbc:mysql://localhost:3306/student?useunicuee=true& characterEncoding=utf8";
  5.     static String username = "root";
  6.     static String password = "123456";
  7.     static Connection  conn = null;
  8.     static ResultSet rs = null;
  9.     static PreparedStatement ps =null;
  10.     public static void init(){
  11.         try {
  12.             Class.forName("com.mysql.jdbc.Driver");
  13.             conn = DriverManager.getConnection(url,username,password);
  14.         } catch (Exception e) {
  15.             System.out.println("init [SQL驱动程序初始化失败!]");
  16.             e.printStackTrace();
  17.         }
  18.     }
  19.     public static int addUpdDel(String sql){
  20.         int i = 0;
  21.         try {
  22.             PreparedStatement ps =  conn.prepareStatement(sql);
  23.             i =  ps.executeUpdate();
  24.         } catch (SQLException e) {
  25.             System.out.println("sql数据库增删改异常");
  26.             e.printStackTrace();
  27.         }
  28.         
  29.         return i;
  30.     }
  31.     public static ResultSet selectSql(String sql){
  32.         try {
  33.             ps =  conn.prepareStatement(sql);
  34.             rs =  ps.executeQuery(sql);
  35.         } catch (SQLException e) {
  36.             System.out.println("sql数据库查询异常");
  37.             e.printStackTrace();
  38.         }
  39.         return rs;
  40.     }
  41.     public static void closeConn(){
  42.         try {
  43.             conn.close();
  44.         } catch (SQLException e) {
  45.             System.out.println("sql数据库关闭异常");
  46.             e.printStackTrace();
  47.         }
  48.     }
  49. }
复制代码
 
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.1"     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3.                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.                         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  5.   <display-name></display-name>   
  6.   
  7.   <filter>
  8.       <filter-name>EncodingFilter</filter-name>
  9.       <filter-class>com.filter.EncodingFilter</filter-class>
  10.   </filter>
  11.   <filter-mapping>
  12.       <filter-name>EncodingFilter</filter-name>
  13.       <url-pattern>/*</url-pattern>
  14.   </filter-mapping>
  15.   
  16.   
  17.   
  18.   <servlet>
  19.     <servlet-name>DengluServlet</servlet-name>
  20.     <servlet-class>com.servlet.DengluServlet</servlet-class>
  21.   </servlet>
  22.   <servlet>
  23.     <servlet-name>ZhuceServlet</servlet-name>
  24.     <servlet-class>com.servlet.ZhuceServlet</servlet-class>
  25.   </servlet>
  26.   <servlet>
  27.     <servlet-name>Searchall</servlet-name>
  28.     <servlet-class>com.servlet.Searchall</servlet-class>
  29.   </servlet>
  30.   <servlet>
  31.     <servlet-name>DeleteServlet</servlet-name>
  32.     <servlet-class>com.servlet.DeleteServlet</servlet-class>
  33.   </servlet>
  34.   <servlet>
  35.     <servlet-name>UpdateServlet</servlet-name>
  36.     <servlet-class>com.servlet.UpdateServlet</servlet-class>
  37.   </servlet>
  38.   <servlet-mapping>
  39.     <servlet-name>DengluServlet</servlet-name>
  40.     <url-pattern>/DengluServlet</url-pattern>
  41.   </servlet-mapping>
  42.   <servlet-mapping>
  43.     <servlet-name>ZhuceServlet</servlet-name>
  44.     <url-pattern>/ZhuceServlet</url-pattern>
  45.   </servlet-mapping>
  46.   <servlet-mapping>
  47.     <servlet-name>Searchall</servlet-name>
  48.     <url-pattern>/Searchall</url-pattern>
  49.   </servlet-mapping>
  50.   <servlet-mapping>
  51.     <servlet-name>DeleteServlet</servlet-name>
  52.     <url-pattern>/DeleteServlet</url-pattern>
  53.   </servlet-mapping>
  54.   <servlet-mapping>
  55.     <servlet-name>UpdateServlet</servlet-name>
  56.     <url-pattern>/UpdateServlet</url-pattern>
  57.   </servlet-mapping>
  58.   
  59.   
  60.   
  61.   <welcome-file-list>
  62.    
  63.     <welcome-file>denglu.jsp</welcome-file>
  64.     <welcome-file>zhuce.jsp</welcome-file>
  65.     <welcome-file>index.jsp</welcome-file>
  66.     <welcome-file>success.jsp</welcome-file>
  67.   </welcome-file-list>
  68. </web-app>
复制代码
 
denglu.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>
  5.     <title>登录注册页面</title>
  6.   </head>
  7.   <body >
  8.           <p>用户登录</p>
  9.          <form action="DengluServlet"  method="post"  style="padding-top:-700px;">
  10.         用户名:<input type="text" name="id" value=""><br><br>
  11.          密码:  <input type="password" name="pwd" value=""><br><br>
  12.                     <input type="submit"value="登录"name="denglu"><input type="reset"value="重置"><br>
  13.      </form>
  14.      <form action="zhuce.jsp">
  15.          <input type="submit"value="新用户注册">
  16.          </form>
  17.    
  18.   </body>
  19. </html>
复制代码
 
index.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <title>My JSP 'index.jsp' starting page</title>
  10.   </head>
  11.   <body>
  12.             <h1>失敗</h1>
  13.   </body>
  14. </html>
复制代码
 
showall.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     <title>所有用户页面</title>
  12.   </head>
  13.   
  14.   <body>
  15.   <h1>${xiaoxi}</h1>
  16.   <table  width="600" border="1" cellpadding="0" >
  17.           <tr>
  18.               <th>用户名</th>
  19.               <th>姓名</th>
  20.               <th>性别</th>
  21.               <th>生日</th>
  22.               <th>密码</th>
  23.               
  24.           </tr>
  25.      <c:forEach var="U" items="${stuAll}"  >
  26.       <form action="UpdateServlet" method="post">
  27.       
  28.        <tr>
  29.            <td><input type="text" value="${U.id}" name="id" ></td>
  30.            <td><input type="text" value="${U.name}" name="name"></td>
  31.            <td><input type="text" value="${U.sex}" name="sex"></td>
  32.            <td><input type="text" value="${U.date}" date="date"></td>
  33.            <td><input type="text" value="${U.pwd}" name="pwd"></td>
  34.            
  35.            
  36.            <td><a target="_blank" href="https://www.cnblogs.com/DeleteServlet?id=${U.id}">删除</a>  <input type="submit" value="更新"/></td>
  37.        </tr>
  38.     </form>
  39.     </c:forEach>  
  40.     </table>
  41.   </body>
  42. </html>
复制代码
 
success.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <title>My JSP 'success.jsp' starting page</title>
  10.   </head>
  11.   <body>
  12.             ${xiaoxi} <br>  
  13.             <a target="_blank" href="https://www.cnblogs.com/Searchall">查看所有用户</a>
  14.   </body>
  15. </html>
复制代码
 
zhuce.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <title>My JSP 'BB.jsp' starting page</title>
  10.   </head>
  11.   <body >
  12.   <form action="ZhuceServlet"method="post" style="padding-top:-700px;">
  13.          
  14.        输入用户名:
  15.        <input name="id" type="text"><br><br>
  16.       
  17.        输入姓名:
  18.        <input name="name" type="text"><br><br>
  19.       
  20.        选择性别:
  21.        <input type="radio" name="sex" value="男"checked>男
  22.        <input type="radio" name="sex"value="女">女<br><br>
  23.       
  24.        输入生日:
  25.        <input name="date"  type="text"><br><br>
  26.        输入密码:
  27.        <input name="pwd"  type="password"><br><br>
  28.        <input type="reset"value="重置"><input type="submit"value="注册">
  29.    </form>
  30.   </body>
  31. </html>
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4