@[TOC]
一、模式分析
在这里插入图片描述
二、架构设计
在这里插入图片描述
三、代码框架
在这里插入图片描述
在这里插入图片描述
四、数据库设置
在这里插入图片描述
五、核心代码
1.User.java
public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.UserDAO.java
public class UserDAO { public static final String DRIVER = "com.mysql.cj.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC";//在这里将3306改为自己的数据库端口号 public static final String DBUSER = "root";//在这里更改为自己数据库的用户名 public static final String DBPASS = "123456";//在这里更改为自己数据库的密码 private Connection conn = null; private PreparedStatement pStat = null; private ResultSet rs = null; public Connection getConnectionn() { try { Class.forName(DRIVER); return DriverManager.getConnection(DBURL, DBUSER, DBPASS); } catch (Exception e) { return null; } } public void close() { try { if (rs != null) rs.close(); if (pStat != null) pStat.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } // end close public boolean isUsernameExists(String username) { conn = getConnectionn(); try { pStat = conn.prepareStatement("select * from users where username=?"); pStat.setString(1, username); rs = pStat.executeQuery(); if (rs.next()) return true; else return false; } catch (Exception e) { return false; } finally { close(); } } // end isUsernameExists public boolean addUser(User user) { conn = getConnectionn(); try { pStat = conn.prepareStatement("insert into users values(null,?,?)"); pStat.setString(1, user.getUsername()); pStat.setString(2, user.getPassword()); int cnt = pStat.executeUpdate(); if (cnt > 0) return true; else return false; } catch (Exception e) { return false; } finally { close(); } } // end add public boolean findUser(String username, String password) { conn = getConnectionn(); try { pStat = conn.prepareStatement("select * from users where username=? and password=?"); pStat.setString(1, username); pStat.setString(2, password); rs = pStat.executeQuery(); if (rs.next()) return true; else return false; } catch (Exception e) { return false; } finally { close(); } } // end findUse }//end class
3.LoginServlet.java
// TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); UserDAO userdao = new UserDAO(); boolean flag = userdao.findUser(username, password); if (flag) { request.getSession().setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { request.getSession().setAttribute("err", "用户名或密码不正确!"); response.sendRedirect("login.jsp"); }
4.RegServlet.java
// TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); User user = new User(); user.setUsername(username); user.setPassword(password); UserDAO userdao = new UserDAO(); if (userdao.isUsernameExists(username)) { request.getSession().setAttribute("err", "用户名已存在"); response.sendRedirect("register.jsp"); } else { boolean flag = userdao.addUser(user); if (flag) { request.getSession().setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { request.getSession().setAttribute("err", "注册失败"); response.sendRedirect("register.jsp"); } }
5.login.jsp
<% String errMsg=(String)session.getAttribute("err"); if( errMsg!=null ) { %> <div style="color:red;"><%=errMsg %></div> <% session.removeAttribute("err"); } %> <form action="LoginServlet"> 用户名<input type="text" name="username"><br/> 密码<input type="password" name="password"><br/> <input type="submit" value="登录"> <input type="reset" value="重填"> </form>
6.register.jsp
<% String errMsg=(String)session.getAttribute("err"); if( errMsg!=null ) { %> <div style="color:red;"><%=errMsg %></div> <% session.removeAttribute("err"); }%> <form action="RegServlet"> 用户名<input type="text" name="username"><br/> 密码<input type="password" name="password"><br/> <input type="submit" value="注册"> <input type="reset" value="重填"> </form>
7.welcome.jsp
<% String username=(String)session.getAttribute("username"); if(username!=null&&!username.isEmpty()) { %> 你已经登录,欢迎<%=session.getAttribute("username") %> <% } else{ out.print("你还没有登录,3秒后自动跳往登录页面"); response.setHeader("refresh", "3;url=login.jsp"); } %>
六、运行截图
在这里插入图片描述