动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组合
代码分享链接
https://pan.baidu.com/s/1um0grvptthw9idisiqa6ra 提取码:hx7c
图示
项目结构
1.selectalluser.jsp
1 <%@ page language="java" contenttype="text/html; charset=utf-8" 2 pageencoding="utf-8"%> 3 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 7 <title>insert title here</title> 8 </head> 9 <body> 10 <a href="selectalluserservlet">查询所有用户</a> 11 </body> 12 </html>
2.adduser.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
<a href="add.jsp">增加用户</a>
<table border="1px">
<tr>
<td>用户id</td>
<td>用户姓名</td>
<td>用户密码</td>
<td>用户昵称</td>
<td>用户性别</td>
<td>用户年龄</td>
<td>用户头像</td>
<td>操作</td>
</tr>
<c:foreach var="xyz" items="${requestscope.user }">
<tr>
<td>${xyz.userid }</td>
<td>${xyz.username }</td>
<td>${xyz.password }</td>
<td>${xyz.nikename }</td>
<td>${xyz.sex }</td>
<td>${xyz.age }</td>
<td><img src="${xyz.image }" alt="图片加载失败" height="50px" weight="50px"/></td>
<td>
<a href="updateselectuserbyidservlet?userid=${xyz.userid }">修改</a>
<a href="deleteuserservlet?userid=${xyz.userid }">删除</a>
</td>
</tr>
</c:foreach>
</table>
<!-- 分页 -->
一共${num }页
<a href=""#>首页</a>
<a href=""#>上一页</a>
<a href=""#>下一页</a>
<a href=""#>尾页</a>
</body>
</html>
3.add.jsp
1 <%@ page language="java" contenttype="text/html; charset=utf-8" 2 pageencoding="utf-8"%> 3 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 7 <title>insert title here</title> 8 </head> 9 <body> 10 <form action="adduserservle" method="post"> 11 用户名<input type="text" name="username"><br> 12 密码<input type="text" name="password"><br> 13 昵称<input type="text" name="nikename"><br> 14 性别<input type="text" name="sex"><br> 15 年龄<input type="text" name="age"><br> 16 头像<input type="file" name="image"><br> 17 <input type="submit" value="提交"> 18 </form> 19 </body> 20 </html>
4.updateuser.jsp
1 <%@ page language="java" contenttype="text/html; charset=utf-8" 2 pageencoding="utf-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 8 <title>insert title here</title> 9 </head> 10 <body> 11 12 13 <form action="updateuserservlet" method="post" > 14 用户id:${user.userid } 15 <input type="hidden" name="userid" value="${user.userid }"><br> 16 用户名<input type="text" name="username" value="${user.username }"><br> 17 密码<input type="text" name="password" value="${user.password }"><br> 18 昵称<input type="text" name="nikename" value="${user.nikename }"><br> 19 性别<input type="text" name="sex" value="${user.sex }"><br> 20 年龄<input type="text" name="age" value="${user.age }"><br> 21 头像<input type="file" name="image" value="${user.image }"><br> 22 <input type="submit" value="提交"> 23 </form> 24 25 </body> 26 </html>
5.(com.icss.vo)user.java
1 /** 2 * <p>title: user.java</p> 3 * <p>description: </p> 4 * <p>copyright: copyright (c) 2020</p> 5 * <p>company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 10 package com.icss.vo; 11 12 /** 13 * <p>title: user</p> 14 * <p>description: </p> 15 * @author liuxin 16 * @date 2020年3月27日 17 */ 18 public class user { 19 //数据封装 20 private int userid; 21 private string username; 22 private string password; 23 private string nikename; 24 private string sex; 25 private int age; 26 private string image; 27 /** 28 * @return the userid 29 */ 30 public int getuserid() { 31 return userid; 32 } 33 /** 34 * @param userid the userid to set 35 */ 36 public void setuserid(int userid) { 37 this.userid = userid; 38 } 39 /** 40 * @return the username 41 */ 42 public string getusername() { 43 return username; 44 } 45 /** 46 * @param username the username to set 47 */ 48 public void setusername(string username) { 49 this.username = username; 50 } 51 /** 52 * @return the password 53 */ 54 public string getpassword() { 55 return password; 56 } 57 /** 58 * @param password the password to set 59 */ 60 public void setpassword(string password) { 61 this.password = password; 62 } 63 /** 64 * @return the nikename 65 */ 66 public string getnikename() { 67 return nikename; 68 } 69 /** 70 * @param nikename the nikename to set 71 */ 72 public void setnikename(string nikename) { 73 this.nikename = nikename; 74 } 75 /** 76 * @return the sex 77 */ 78 public string getsex() { 79 return sex; 80 } 81 /** 82 * @param sex the sex to set 83 */ 84 public void setsex(string sex) { 85 this.sex = sex; 86 } 87 /** 88 * @return the age 89 */ 90 public int getage() { 91 return age; 92 } 93 /** 94 * @param age the age to set 95 */ 96 public void setage(int age) { 97 this.age = age; 98 } 99 /** 100 * @return the image 101 */ 102 public string getimage() { 103 return image; 104 } 105 /** 106 * @param image the image to set 107 */ 108 public void setimage(string image) { 109 this.image = image; 110 } 111 112 113 114 }
6.(com.icss.util)dbutil.java
1 package com.icss.util; 2 3 import java.sql.connection; 4 import java.sql.drivermanager; 5 import java.sql.sqlexception; 6 7 public class dbutil { 8 public static connection connection; 9 public static connection getconnection() throws classnotfoundexception, sqlexception{ 10 class.forname("oracle.jdbc.driver.oracledriver"); 11 connection connection = drivermanager.getconnection 12 ("jdbc:oracle:thin:@localhost:1521:xe","数据库名","数据库密码"); 13 return connection; 14 } 15 16 }
7.(com.icss.dao)userdao.java
1 /** 2 * <p>title: userdao.java</p> 3 * <p>description: </p> 4 * <p>copyright: copyright (c) 2020</p> 5 * <p>company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 10 package com.icss.dao; 11 12 import java.sql.preparedstatement; 13 import java.sql.resultset; 14 import java.sql.sqlexception; 15 import java.util.arraylist; 16 17 import org.apache.tomcat.dbcp.dbcp.dbcpexception; 18 19 import com.icss.util.dbutil; 20 import com.icss.vo.user; 21 22 import javafx.css.pseudoclass; 23 24 /** 25 * <p>title: userdao</p> 26 * <p>description: </p> 27 * @author liuxin 28 * @date 2020年3月27日 29 */ 30 public class userdao { 31 //查询所有用户 32 public arraylist<user> selectalluserdao() throws classnotfoundexception, sqlexception{ 33 preparedstatement ps = dbutil.getconnection().preparestatement 34 ("select * from user_0327"); 35 resultset rs = ps.executequery(); 36 arraylist<user> al = new arraylist<user>(); 37 while(rs.next()){ 38 user user = new user(); 39 user.setuserid(rs.getint(1)); 40 user.setusername(rs.getstring(2)); 41 user.setpassword(rs.getstring(3)); 42 user.setnikename(rs.getstring(4)); 43 user.setsex(rs.getstring(5)); 44 user.setage(rs.getint(6)); 45 user.setimage(rs.getstring(7)); 46 al.add(user); 47 } 48 ps.close(); 49 rs.close(); 50 dbutil.getconnection().close(); 51 return al; 52 } 53 //增加用户 54 public void adduserdao(user user) throws classnotfoundexception, sqlexception{ 55 preparedstatement ps = dbutil.getconnection().preparestatement 56 ("insert into user_0327 values(user0327_seq.nextval,?,?,?,?,?,?)"); 57 ps.setstring(1, user.getusername()); 58 ps.setstring(2, user.getpassword()); 59 ps.setstring(3, user.getnikename()); 60 ps.setstring(4, user.getsex()); 61 ps.setint(5, user.getage()); 62 //这里先将头像照片定死,没有使用上传图片 63 ps.setstring(6, "image/"+user.getimage()); 64 ps.executeupdate(); 65 ps.close(); 66 dbutil.getconnection().close(); 67 } 68 //删除用户 69 public void deleteuserdao(int id) throws classnotfoundexception, sqlexception{ 70 preparedstatement ps = dbutil.getconnection().preparestatement 71 ("delete from user_0327 where userid=?"); 72 ps.setint(1, id); 73 ps.executequery(); 74 ps.close(); 75 dbutil.getconnection().close(); 76 } 77 //修改之查询 78 public user updateselectuserbyiddao(int id) throws classnotfoundexception, sqlexception{ 79 preparedstatement ps = dbutil.getconnection().preparestatement 80 ("select * from user_0327 where userid=?"); 81 ps.setint(1, id); 82 resultset rs = ps.executequery(); 83 user user = new user(); 84 while(rs.next()){ 85 user.setuserid(rs.getint(1)); 86 user.setusername(rs.getstring(2)); 87 user.setpassword(rs.getstring(3)); 88 user.setnikename(rs.getstring(4)); 89 user.setsex(rs.getstring(5)); 90 user.setage(rs.getint(6)); 91 user.setimage(rs.getstring(7)); 92 } 93 ps.close(); 94 rs.close(); 95 dbutil.getconnection().close(); 96 return user; 97 } 98 //修改之修改 99 public void updateuserbyiddao(user user) throws classnotfoundexception, sqlexception{ 100 preparedstatement ps = dbutil.getconnection().preparestatement 101 ("update user_0327 set username=?,password=?,nikename=?,sex=?,age=?,image=? where userid =?"); 102 ps.setstring(1, user.getusername()); 103 ps.setstring(2, user.getpassword()); 104 ps.setstring(3, user.getnikename()); 105 ps.setstring(4, user.getsex()); 106 ps.setint(5, user.getage()); 107 ps.setstring(6, "image"+user.getimage()); 108 ps.setint(7, user.getuserid()); 109 ps.executeupdate(); 110 ps.close(); 111 dbutil.getconnection().close(); 112 } 113 //获得分页数量 114 public int gettotalpagedao(int pagesize) throws classnotfoundexception, sqlexception{ 115 preparedstatement ps = dbutil.getconnection().preparestatement 116 ("select count(userid) c from user_0327"); 117 resultset rs = ps.executequery(); 118 int total =0; 119 if(rs.next()){ 120 int num = rs.getint("c"); 121 total = num%pagesize==0?num/pagesize:num/pagesize+1; 122 } 123 ps.close(); 124 dbutil.getconnection().close(); 125 return total; 126 } 127 }
8.(com.icss.service)userservice.java
1 /** 2 * <p>title: userservice.java</p> 3 * <p>description: </p> 4 * <p>copyright: copyright (c) 2020</p> 5 * <p>company: </p> 6 * @author liuxin 7 * @date 2020年3月27日 8 * @version 1.0 9 */ 10 package com.icss.service; 11 12 import java.sql.sqlexception; 13 import java.util.arraylist; 14 15 import com.icss.dao.userdao; 16 import com.icss.vo.user; 17 18 /** 19 * <p>title: userservice</p> 20 * <p>description: </p> 21 * @author liuxin 22 * @date 2020年3月27日 23 */ 24 public class userservice { 25 public arraylist<user> selectalluserservice() throws classnotfoundexception, sqlexception{ 26 userdao user = new userdao(); 27 return user.selectalluserdao(); 28 } 29 public void adduserservice(user user) throws classnotfoundexception, sqlexception{ 30 userdao ud = new userdao(); 31 ud.adduserdao(user); 32 } 33 public void deleteuserservice(int id) throws classnotfoundexception, sqlexception{ 34 userdao ud = new userdao(); 35 ud.deleteuserdao(id); 36 } 37 public user updateselectuserbyidservice(int id) throws classnotfoundexception, sqlexception{ 38 userdao ud = new userdao(); 39 return ud.updateselectuserbyiddao(id); 40 } 41 public void updateuserbyidservice(user user) throws classnotfoundexception, sqlexception{ 42 userdao ud = new userdao(); 43 ud.updateuserbyiddao(user); 44 } 45 public int gettotalpageservice(int pagesize) throws classnotfoundexception, sqlexception{ 46 userdao ud = new userdao(); 47 return ud.gettotalpagedao(pagesize); 48 } 49 50 }
9.(com.icss.controller)selectalluserservlet.java
1 package com.icss.controller; 2 3 import java.io.ioexception; 4 import java.sql.sqlexception; 5 import java.util.arraylist; 6 7 import javax.servlet.servletexception; 8 import javax.servlet.annotation.webservlet; 9 import javax.servlet.http.httpservlet; 10 import javax.servlet.http.httpservletrequest; 11 import javax.servlet.http.httpservletresponse; 12 13 import com.icss.service.userservice; 14 import com.icss.vo.user; 15 16 /** 17 * servlet implementation class selectalluserservlet 18 */ 19 @webservlet("/selectalluserservlet") 20 public class selectalluserservlet extends httpservlet { 21 private static final long serialversionuid = 1l; 22 23 /** 24 * @see httpservlet#httpservlet() 25 */ 26 public selectalluserservlet() { 27 super(); 28 // todo auto-generated constructor stub 29 } 30 31 /** 32 * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) 33 */ 34 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 35 dopost(request, response); 36 } 37 38 /** 39 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) 40 */ 41 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 42 request.setcharacterencoding("utf-8"); 43 //接受请求 44 //处理请求 45 userservice us = new userservice(); 46 try { 47 //************分页数************** 48 int pagesize = 3; 49 int num = us.gettotalpageservice(pagesize); 50 system.out.println(num); 51 request.setattribute("num", num); 52 //************分页数************** 53 arraylist<user> user = us.selectalluserservice(); 54 request.setattribute("user", user); 55 request.getrequestdispatcher("adduser.jsp").forward(request, response); 56 } catch (classnotfoundexception e) { 57 // todo auto-generated catch block 58 e.printstacktrace(); 59 } catch (sqlexception e) { 60 // todo auto-generated catch block 61 e.printstacktrace(); 62 } 63 64 } 65 66 }
10.(com.icss.controller)adduserservlet.java
1 package com.icss.controller; 2 3 import java.io.ioexception; 4 import java.sql.sqlexception; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.annotation.webservlet; 8 import javax.servlet.http.httpservlet; 9 import javax.servlet.http.httpservletrequest; 10 import javax.servlet.http.httpservletresponse; 11 12 import com.icss.service.userservice; 13 import com.icss.vo.user; 14 15 /** 16 * servlet implementation class adduserservle 17 */ 18 @webservlet("/adduserservle") 19 public class adduserservle extends httpservlet { 20 private static final long serialversionuid = 1l; 21 22 /** 23 * @see httpservlet#httpservlet() 24 */ 25 public adduserservle() { 26 super(); 27 // todo auto-generated constructor stub 28 } 29 30 /** 31 * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) 32 */ 33 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 34 dopost(request, response); 35 } 36 37 /** 38 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) 39 */ 40 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 41 request.setcharacterencoding("utf-8"); 42 //接受请求 43 user user = new user(); 44 //string user2 = request.getparameter("username"); 45 user.setusername(request.getparameter("username")); 46 ///system.out.println(user2); 47 user.setpassword(request.getparameter("password")); 48 user.setnikename(request.getparameter("nikename")); 49 user.setsex(request.getparameter("sex")); 50 user.setage(integer.parseint(request.getparameter("age"))); 51 user.setimage(request.getparameter("image")); 52 //处理请求 53 userservice us = new userservice(); 54 try { 55 56 us.adduserservice(user); 57 request.getrequestdispatcher("selectalluserservlet").forward(request, response); 58 } catch (classnotfoundexception e) { 59 // todo auto-generated catch block 60 e.printstacktrace(); 61 } catch (sqlexception e) { 62 // todo auto-generated catch block 63 e.printstacktrace(); 64 } 65 } 66 67 }
11.(com.icss.controller)updateselectuserbyidservlet.java
1 package com.icss.controller; 2 3 import java.io.ioexception; 4 import java.sql.sqlexception; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.annotation.webservlet; 8 import javax.servlet.http.httpservlet; 9 import javax.servlet.http.httpservletrequest; 10 import javax.servlet.http.httpservletresponse; 11 12 import com.icss.service.userservice; 13 import com.icss.vo.user; 14 15 /** 16 * servlet implementation class updateselectuserbyidservlet 17 */ 18 @webservlet("/updateselectuserbyidservlet") 19 public class updateselectuserbyidservlet extends httpservlet { 20 private static final long serialversionuid = 1l; 21 22 /** 23 * @see httpservlet#httpservlet() 24 */ 25 public updateselectuserbyidservlet() { 26 super(); 27 // todo auto-generated constructor stub 28 } 29 30 /** 31 * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) 32 */ 33 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 34 dopost(request, response); 35 } 36 37 /** 38 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) 39 */ 40 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 41 request.setcharacterencoding("utf-8"); 42 //接受数据 43 int id = integer.parseint(request.getparameter("userid")); 44 //system.out.println(id); 45 //处理数据 46 userservice us = new userservice(); 47 try { 48 user user = us.updateselectuserbyidservice(id); 49 request.setattribute("user", user); 50 request.getrequestdispatcher("updateuser.jsp").forward(request, response); 51 } catch (classnotfoundexception e) { 52 // todo auto-generated catch block 53 e.printstacktrace(); 54 } catch (sqlexception e) { 55 // todo auto-generated catch block 56 e.printstacktrace(); 57 } 58 } 59 60 }
12.(com.icss.controller)updateuserservlet.java
1 package com.icss.controller; 2 3 import java.io.ioexception; 4 import java.sql.sqlexception; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.annotation.webservlet; 8 import javax.servlet.http.httpservlet; 9 import javax.servlet.http.httpservletrequest; 10 import javax.servlet.http.httpservletresponse; 11 12 import com.icss.service.userservice; 13 import com.icss.vo.user; 14 15 /** 16 * servlet implementation class updateuserservlet 17 */ 18 @webservlet("/updateuserservlet") 19 public class updateuserservlet extends httpservlet { 20 private static final long serialversionuid = 1l; 21 22 /** 23 * @see httpservlet#httpservlet() 24 */ 25 public updateuserservlet() { 26 super(); 27 // todo auto-generated constructor stub 28 } 29 30 /** 31 * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) 32 */ 33 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 34 dopost(request, response); 35 } 36 37 /** 38 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) 39 */ 40 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 41 request.setcharacterencoding("utf-8"); 42 //接受数据 43 user user = new user(); 44 user.setuserid(integer.parseint(request.getparameter("userid"))); 45 user.setusername(request.getparameter("username")); 46 user.setpassword(request.getparameter("password")); 47 user.setnikename(request.getparameter("nikename")); 48 user.setsex(request.getparameter("sex")); 49 user.setage(integer.parseint(request.getparameter("age"))); 50 user.setimage(request.getparameter("image")); 51 //处理请求 52 userservice us = new userservice(); 53 try { 54 us.updateuserbyidservice(user); 55 request.getrequestdispatcher("selectalluserservlet").forward(request, response); 56 } catch (classnotfoundexception e) { 57 // todo auto-generated catch block 58 e.printstacktrace(); 59 } catch (sqlexception e) { 60 // todo auto-generated catch block 61 e.printstacktrace(); 62 } 63 } 64 65 }
13.(com.icss.controller)deleteuserservlet.java
1 package com.icss.controller; 2 3 import java.io.ioexception; 4 import java.sql.sqlexception; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.annotation.webservlet; 8 import javax.servlet.http.httpservlet; 9 import javax.servlet.http.httpservletrequest; 10 import javax.servlet.http.httpservletresponse; 11 12 import com.icss.service.userservice; 13 14 /** 15 * servlet implementation class deleteuserservlet 16 */ 17 @webservlet("/deleteuserservlet") 18 public class deleteuserservlet extends httpservlet { 19 private static final long serialversionuid = 1l; 20 21 /** 22 * @see httpservlet#httpservlet() 23 */ 24 public deleteuserservlet() { 25 super(); 26 // todo auto-generated constructor stub 27 } 28 29 /** 30 * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) 31 */ 32 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 33 dopost(request, response); 34 } 35 36 /** 37 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) 38 */ 39 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 40 request.setcharacterencoding("utf-8"); 41 //接受请求 42 int id = integer.parseint(request.getparameter("userid")); 43 //system.out.println(id); 44 //处理请求 45 userservice us = new userservice(); 46 try { 47 us.deleteuserservice(id); 48 request.getrequestdispatcher("selectalluserservlet").forward(request, response); 49 } catch (classnotfoundexception | sqlexception e) { 50 // todo auto-generated catch block 51 e.printstacktrace(); 52 } 53 } 54 55 }
上一篇: 截图小工具开发笔记