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; } }
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
// 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"); }
// 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"); } }
<% 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>
<% 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>
<% 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"); } %>
CSDN下载:JSP+Servlet+JavaBean模式实现登录注册
如需要本资源下载也可以直接添加作者QQ,并可以获取资源预览、软件安装、工程导入、协助运行、算法解释、报告协助撰写等售后服务
欢迎交流
QQ:402501817(心源易码 佩奇)
QQ:2215991436(心源易码 派大星)