基于DAO模式和MVC模式完成一个用户管理程序,包括用户注册、用户登录、浏览用户列表和删除用户等四个功能。
要求:1)JSP页面中不允许出现Java代码,使用标签实现显示逻辑
2)HTTP请求发送给Servlet,由Servlet负责调用DAO类,并转发给JSP页面
3)浏览用户列表页面要支持分页功能
DBCon.java类:与数据库admin表连接,有插入,删除等动作;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package db_admin; 2 3 import java.sql.*; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class DBCon { 10 private String dbDriver = "com.mysql.jdbc.Driver"; 11 private String dbUrl = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8"; 12 private String dbUser = "root"; 13 private String dbPwd = "123456"; 14 private Connection conn = null; 15 16 public Connection getConnection() { 17 try { 18 Class.forName(dbDriver); 19 conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd); 20 } catch (ClassNotFoundException e) { 21 e.printStackTrace(); 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 public ResultSet doQurey(String sql) { 29 ResultSet rs = null; 30 conn = this.getConnection(); 31 try { 32 Statement stmt = conn.createStatement(); 33 rs = stmt.executeQuery(sql); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 return rs; 38 } 39 40 public int doUpdate(String sql) { 41 int res = 0; 42 conn = this.getConnection(); 43 try { 44 Statement stmt = conn.createStatement(); 45 res = stmt.executeUpdate(sql); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 return res; 50 } 51 52 public String checkUser(String username, String password) { 53 boolean has_username = false; 54 boolean password_correct = false; 55 ResultSet rs = null; 56 try { 57 rs = doQurey("select * from admin"); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 62 try { 63 while (rs.next()) { 64 String temp_username = rs.getString("username"); 65 String temp_password = rs.getString("password"); 66 if (username.equals(temp_username)) { 67 has_username = true; 68 if (password.equals(temp_password)) { 69 password_correct = true; 70 71 return "hasUserNameAndPasswordCorrect"; 72 73 } 74 75 return "hasUserNameButPasswordInCorrect"; 76 77 } 78 79 } 80 81 } catch (SQLException e) { 82 83 e.printStackTrace(); 84 85 } 86 87 return "hasNoUserName"; 88 89 } 90 91 public void close() throws SQLException { 92 try { 93 conn.close(); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 } 97 } 98 }
LoginControl.java:Servlet类,做登录验证,查看用户是否存在,密码是否正确;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package servletLogin; 2 3 import java.io.IOException; 4 import javax.servlet.RequestDispatcher; 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import db_admin.DBCon; 12 13 /** 14 * Servlet implementation class LoginControl 15 */ 16 @WebServlet("/LoginControl") 17 public class LoginControl extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public LoginControl() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 30 * response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 String username = request.getParameter("username"); 36 String password = request.getParameter("password"); 37 38 DBCon dbc = new DBCon(); 39 String result = dbc.checkUser(username, password); 40 if (result.equals("hasUserNameAndPasswordCorrect")) { 41 System.out.println("用户名及密码正确"); 42 RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); 43 rd.forward(request, response); 44 } else if (result.equals("hasUserNameButPasswordInCorrect")) { 45 System.out.println("密码不正确"); 46 RequestDispatcher rd = request.getRequestDispatcher("error.jsp"); 47 rd.forward(request, response); 48 } else if (result.equals("hasNoUserName")) { 49 System.out.println("该用户不存在"); 50 RequestDispatcher rd = request.getRequestDispatcher("error2.jsp"); 51 rd.forward(request, response); 52 } 53 54 55 } 56 57 /** 58 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 59 * response) 60 */ 61 protected void doPost(HttpServletRequest request, HttpServletResponse response) 62 throws ServletException, IOException { 63 // TODO Auto-generated method stub 64 doGet(request, response); 65 } 66 67 }
regist.jsp:注册页面;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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>注册页面</title> 8 <style> 9 @charset "utf-8"; 10 #frm { 11 padding: 20px; 12 margin-top: 10px; 13 height: 1000px; 14 background-color: #F0FFFF; 15 } 16 #main{ 17 margin-top:30px; 18 margin-left:200px; 19 background-color: #FFDAB9; 20 border:1px solid; 21 padding:10px 40px; 22 width:300px; 23 overflow:auto; 24 text-align:center; 25 } 26 .divcls { 27 border-bottom: 1px dashed blue; 28 margin-bottom: 10pt; 29 padding-bottom: 10pt; 30 } 31 #mytop{ 32 text-align:left; 33 margin-bottom:10pt; 34 font-size:15pt; 35 } 36 #mytop a{ 37 font-size:10pt; 38 color:bule; 39 } 40 #mytop span{ 41 font-size:10px; 42 font-weight:bolder; 43 } 44 .info { 45 margin-left: 25%; 46 padding: 14px 16px; 47 height: 1000px; 48 } 49 .myrow { 50 margin: 10pt 0; 51 } 52 .myrow label { 53 display: block; 54 width: 100pt; 55 font-weight: bolder; 56 } 57 .myrow note { 58 font-size: 10px; 59 font-weight: bloder; 60 } 61 .noignore { 62 color: red; 63 } 64 .submit { 65 style:width:100%; 66 text-align: center; 67 } 68 .agree { 69 color: red; 70 font-size: 15px; 71 } 72 </style> 73 <script type="text/javascript"> 74 function nme(){ 75 var result = true; 76 var nameDOM = document.getElementById('uname').value; 77 if (nameDOM == '') { 78 document.getElementById('name').innerHTML = '<span style="color:red;">用户名不能为空!</span>'; 79 result = false; 80 }else { 81 document.getElementById('name').innerHTML = '<span style="color:green;">正确!</span>'; 82 result = true; 83 } 84 85 return result; 86 } 87 /*密码验证*/ 88 function Password(){ 89 var result = true; 90 var pwdDOM = document.getElementById('upwd').value; 91 if (pwdDOM == ''){ 92 document.getElementById('pwd').innerHTML = '<span style="color:red;">密码不能为空!</span>'; 93 result = false; 94 }else { 95 document.getElementById('pwd').innerHTML = '<span style="color:green;">正确!</span>'; 96 result = true; 97 } 98 return result; 99 } 100 /*确认密码验证*/ 101 function rePassword(){ 102 var result = true; 103 var pwdDOM = document.getElementById('upwd').value; 104 var repwdDOM = document.getElementById('reupwd').value; 105 if (repwdDOM == ''){ 106 document.getElementById('repwd').innerHTML = '<span style="color:red;">此项不能为空!</span>'; 107 result = false; 108 }else { 109 document.getElementById('repwd').innerHTML = '<span style="color:green;">正确!</span>'; 110 result = true; 111 } 112 return result; 113 } 114 function Rabbit(){ 115 var Rabit = new Array(); 116 Rabit[0] = nme(); 117 Rabit[1] = Password(); 118 return Rabit; 119 120 } 121 function myFunction(){ 122 var w = new Array(); 123 w = Rabbit(); 124 var a = 0; 125 var s = 1; 126 for (var i = 0; i < 2; i++) { 127 if(w[i]==true){ 128 a++;console.warn(a); 129 } 130 console.warn(w[i]); 131 } 132 133 if (a == 2) { 134 alert("注册成功"); 135 } 136 else{ 137 alert('请按规定输入完整的注册资料!'); 138 var Rabit; 139 Rabit = Password(); 140 if (Rabit == false) { 141 var passwordfocus = document.getElementById('password'); 142 passwordfocus.focus(); 143 } 144 Rabit = nme(); 145 if (Rabit == false) { 146 var codefocus = document.getElementById('name'); 147 codefocus.focus(); 148 } 149 } 150 } 151 </script> 152 </head> 153 <body> 154 <form id="frm" action="do_insert.jsp" method="post"> 155 <div id="mytop" class="divcls">用户注册: 156 <a href="login.jsp">已注册,点击登录</a> 157 </div> 158 <div id="main"> 159 <div class="myrow"> 160 <label for="uname">用户名 :</label> 161 <input type="text" id="uname" name="username" onClick="nme()"/> 162 <span class="noignore">*</span> <span class="note" id="name">必填</span> </div> 163 <div class="myrow"> 164 <label for="upwd">密 码:</label> 165 <input type="password" id="upwd" name="password" onClick="Password()"/> 166 <span class="noignore">*</span> <span class="note" id="pwd">必填</span> </div> 167 <div class="submit"> 168 <input id="mysubmit" type="submit" value="确认注册" onClick="myFunction()"/> 169 <input id="mysubmit" type="reset" value="重置"/> 170 </div> 171 </div> 172 </form> 173 </body> 174 </html>
do_insert.jsp:将输入的用户名与密码插入到数据库的admin表中;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <%@ page import="db_admin.DBCon,java.sql.*" %> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 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>添加用户</title> 9 </head> 10 <body> 11 <% 12 response.setCharacterEncoding("utf-8"); 13 String username = request.getParameter("username"); 14 String password = request.getParameter("password"); 15 DBCon dbc = new DBCon(); 16 String sql = "insert into admin(username,password)values('"+ username +"','"+ password +"')"; 17 int res = dbc.doUpdate(sql); 18 if(res>0){ 19 out.print("<script>alert('注册成功,单击跳转到登录页面!')</script>"); 20 response.setHeader("refresh", "1;url='login.jsp'"); 21 }else{ 22 out.print("<script>alert('注册失败,单击跳转到注册页面!')</script>"); 23 response.sendRedirect("regist.jsp"); 24 } 25 %> 26 </body> 27 </html>
login.jsp:登录页面;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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>登录页面</title> 8 <style> 9 @charset "utf-8"; 10 #frm { 11 padding: 20px; 12 margin-top: 10px; 13 height: 1000px; 14 background-color: #F0FFFF; 15 } 16 #main{ 17 margin-top:30px; 18 margin-left:200px; 19 background-color: #FFDAB9; 20 border:1px solid; 21 padding:10px 40px; 22 width:300px; 23 overflow:auto; 24 text-align:center; 25 } 26 .divcls { 27 border-bottom: 1px dashed blue; 28 margin-bottom: 10pt; 29 padding-bottom: 10pt; 30 } 31 #mytop{ 32 text-align:left; 33 margin-bottom:10pt; 34 font-size:15pt; 35 } 36 #mytop a{ 37 font-size:10pt; 38 } 39 #mytop span{ 40 font-size:10px; 41 font-weight:bolder; 42 } 43 .myrow { 44 margin: 10pt 0; 45 } 46 .myrow label { 47 display: block; 48 width: 100pt; 49 font-weight: bolder;` 50 } 51 .myrow note { 52 font-size: 10px; 53 font-weight: bloder; 54 } 55 .noignore { 56 color: red; 57 } 58 .submit { 59 style:width:100%; 60 text-align: center; 61 } 62 .agree { 63 color: red; 64 font-size: 15px; 65 } 66 </style> 67 </head> 68 <body> 69 <form id="frm" action="LoginControl" method="post"> 70 71 <div id="mytop" class="divcls">用户登录: 72 <a href="regist.jsp">未注册,点击注册</a> 73 </div> 74 <div id="main"> 75 <div class="myrow"> 76 <label for="uname">用户名 :</label> 77 <input type="text" id="uname" name="username" onClick="nme()"/> 78 </div> 79 <div class="myrow"> 80 <label for="upwd">密 码:</label> 81 <input type="password" id="upwd" name="password" onClick="Password()"/> 82 </div> 83 <div class="submit"> 84 <input id="mysubmit" type="submit" value="登录"/> 85 <input id="mysubmit" type="reset" value="重置"/> 86 </div> 87 </div> 88 </form> 89 </body> 90 </html>
pager.jsp:分页显示admin中的数据;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <%@page import="java.sql.*,db_admin.*"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 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>分页显示</title> 9 <style type="text/css"> 10 body{ 11 background-color: #FFDAB9; 12 } 13 body.table{ 14 margin-top:30px; 15 margin-left:200px; 16 } 17 </style> 18 </head> 19 <body> 20 <% 21 //总的页面数 22 int pageCount = 0; 23 //每页显示的记录数 24 int pageSize = 5; 25 //当前页面 26 int currentP = 1; 27 //表中的记录数 28 int totalCount = 5; 29 //数据库的连接 30 DBCon dbc = new DBCon(); 31 //表中总记录数 32 String sql = "select count(*) as t from admin"; 33 ResultSet rs = dbc.doQurey(sql); 34 if (rs.next()) 35 totalCount = rs.getInt("t"); 36 //根据总的记录数来计算页面数 37 if (totalCount / pageSize == 0) 38 pageCount = (totalCount / pageSize); 39 else 40 pageCount = (totalCount / pageSize) + 1; 41 //获得分页条上的当前页码 42 String pStr = request.getParameter("p"); 43 if (pStr == null) 44 pStr = "1"; 45 currentP = Integer.parseInt(pStr); 46 //如果当前页大于总的页面数,当前页面赋值为总的页面数 47 if (currentP > pageCount) 48 currentP = pageCount; 49 //如果当前页小于0,重置为第一页 50 if (currentP < 0) 51 currentP = 1; 52 //分页查询,MySQL分页查询关键字limit,注意limit后有空格 53 sql = "select * from admin limit " + (currentP - 1) * pageSize + "," + pageSize; 54 rs = dbc.doQurey(sql); 55 %> 56 <table border="1"> 57 <tr> 58 <th>id</th> 59 <th>用户名</th> 60 <th>密码</th> 61 <th>功能</th> 62 </tr> 63 <% 64 while (rs.next()) { 65 %> 66 67 <tr> 68 <td><%= rs.getInt("id") %></td> 69 <td><%= rs.getString("username") %></td> 70 <td><%= rs.getString("password") %></td> 71 <td><a href="do_edit.jsp?id=<%= rs.getInt("id")%>">编辑</a> 72 <a href="do_delete.jsp?id=<%= rs.getInt("id")%>">删除</a> 73 </td> 74 </tr> 75 <% 76 } 77 %> 78 </table> 79 <% 80 if (currentP > 1) { 81 %> 82 <a href="pager.jsp?p=1">第一页</a> 83 <a href="pager.jsp?p=<%= currentP - 1 %>">上一页</a> 84 <% 85 } 86 %> 87 <% 88 if (currentP <= pageCount) { 89 %> 90 <a href="pager.jsp?p=<%= currentP + 1 %>">下一页</a> 91 <a href="pager.jsp?p=<%= pageCount %>">最后一页</a> 92 <% 93 } 94 %> 95 </body> 96 </html>