废话不多说,先贴代码
先是注册页面:zhuce.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form method="post" action="${pageContext.request.contextPath}/Zhuceservlet?method=add"><!-- method 提交方式 --> <div align="center"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" ></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" ></td> </tr> <tr> <td>邮箱:</td> <td><input type="text" name="email" ></td> </tr> <tr> <td> </td> <td align="center"> <input type="submit" value="注册" ><br> </td> </tr> </table> </div> </form> </body> </html>
然后是登录界面:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/TestDenglu/UserServlet" method="post" ><!-- method 提交方式 --> <div align="center"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" ></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" ></td> </tr> <tr> <td><input type="submit" value="登录" ><br></td> <td><a href="zhuce.jsp">注册账号</a></td> </tr> </table> </div> </form> </body> </html>
主界面就有些简陋了,因为主要是实现注册登录的:welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <b>欢迎您!</b> </div> </body> </html>
servlet实现登录的主要方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String psw =new UserDao().findUsername(username); if(psw ==null){ PrintWriter out = response.getWriter(); out.println(" <script type='text/javascript'> alert('该用户不存在');window.location.href='login.jsp'</script>"); out.println("</HTML>"); out.flush(); out.close(); } if(psw!=null&&!psw.equals(password)){ PrintWriter out = response.getWriter(); out.println(" <script type='text/javascript'> alert('密码错误,请重新录入');window.location.href='login.jsp'</script>"); out.println("</HTML>"); out.flush(); out.close(); } if(psw!=null&&psw.equals(password)){ PrintWriter out = response.getWriter(); out.println(" <script type='text/javascript'> alert('登陆成功!');window.location.href='welcome.jsp'</script>"); out.println("</HTML>"); out.flush(); out.close(); out.print(true); } }
其中findUsername(username)方法
public String findUsername(String username){ String psw = null; Connection con =null; PreparedStatement pstmt =null; ResultSet rs = null; try { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3308/testdenglu?characterEncoding=utf8&useSSL=true"; String user = "root"; String password = "root"; Class.forName(driver); con = DriverManager.getConnection(url, user, password); String sql = "select * from user where username=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, username); rs = pstmt.executeQuery(); if(rs==null){ return null; } if(rs.next()){ psw=rs.getString("password"); }else{ psw=null; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); } catch (SQLException e) { } } return psw; }
扫描二维码关注公众号,回复:
9294015 查看本文章
再说一下我的想法
首先是注册
1.先实现注册的页面及表单内容,例如用户名、密码、邮箱、性别等
2.然后实现用户添加操作,表单提交并实现数据库连接
3.最后是数据库的添加成功
接着是登录
1.先是实现登录界面,表单以及每项标签的name
2.然后是form路径,通过servlet实现查询校验
servlet:
1、先获取当前输入的用户名、密码
2、然后拿着这个用户名通过数据库查询,是否有与之相同的
若没有,则返回空,用户名不存在
若有,则返回该用户在数据库中的密码,然后和当前输入的密码进行校验
若不同,则用户名或密码输入错误!
若相同,则登陆成功!跳转到主界面