案例:自动登录,记住密码(Session+JSP)

- 日理万妓 2022-12-24 11:51 188阅读 0赞
  1. package com.bdit;
  2. import javax.servlet.http.Cookie;
  3. public class DBCookie {
  4. public static Cookie getCookie(Cookie[] cookies,String key){
  5. if(cookies!=null&&cookies.length>0){
  6. for(Cookie cookie:cookies){
  7. if(cookie.getName().equals(key)){
  8. return cookie;
  9. }
  10. }
  11. }
  12. return null;
  13. }
  14. }

servlet

  1. package com.bdit.servlet;
  2. import com.bdit.sevice.TreeSevice;
  3. import com.bdit.vo.Tree;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.Cookie;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. @WebServlet(urlPatterns = {
  12. "/treeServlet"})
  13. public class TreeServlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. doPost(req, resp);
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  20. req.setCharacterEncoding("utf-8");
  21. resp.setContentType("text/html;charset=UTF-8");
  22. String username=req.getParameter("username");
  23. String pass=req.getParameter("pass");
  24. String hobby=req.getParameter("hobby");
  25. String hobby2=req.getParameter("hobby2");
  26. TreeSevice sevice=new TreeSevice();
  27. //根据用户名和密码查找
  28. Tree tree=sevice.find(username,pass);
  29. if(tree!=null&&tree.getUsername().equals(username)&&tree.getPass().equals(pass)){
  30. if(hobby2!=null&&hobby2.equals("2")){
  31. if(hobby!=null&&hobby.equals("1")){
  32. Cookie cookie1=new Cookie("username",username);
  33. Cookie cookie2=new Cookie("pass",pass);
  34. cookie1.setPath(req.getContextPath());
  35. cookie2.setPath(req.getContextPath());
  36. cookie1.setMaxAge(60*60*24);
  37. cookie2.setMaxAge(60*60*24);
  38. resp.addCookie(cookie1);
  39. resp.addCookie(cookie2);
  40. Cookie cookie3=new Cookie("hobby","checked");
  41. cookie3.setPath(req.getContextPath());
  42. cookie3.setMaxAge(60*60*24);
  43. resp.addCookie(cookie3);
  44. Cookie cookie4=new Cookie("hobby2","checked");
  45. cookie4.setPath(req.getContextPath());
  46. cookie4.setMaxAge(15);
  47. resp.addCookie(cookie4);
  48. resp.sendRedirect("success.jsp");
  49. }
  50. else{
  51. resp.sendRedirect("success.jsp");
  52. }
  53. }else{
  54. if(hobby!=null&&hobby.equals("1")) {
  55. Cookie cookie1 = new Cookie("username", username);
  56. Cookie cookie2 = new Cookie("pass", pass);
  57. cookie1.setPath(req.getContextPath());
  58. cookie2.setPath(req.getContextPath());
  59. cookie1.setMaxAge(60 * 60 * 24);
  60. cookie2.setMaxAge(60 * 60 * 24);
  61. resp.addCookie(cookie1);
  62. resp.addCookie(cookie2);
  63. Cookie cookie3 = new Cookie("hobby", "checked");
  64. cookie3.setPath(req.getContextPath());
  65. cookie3.setMaxAge(60 * 60 * 24);
  66. resp.addCookie(cookie3);
  67. }else{
  68. resp.sendRedirect("success.jsp");
  69. }
  70. }
  71. }else if(tree==null){
  72. resp.sendRedirect("error.jsp");
  73. }
  74. }
  75. }

主页面index.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2020/10/20
  5. Time: 21:10
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  10. <%@ page import="com.bdit.DBCookie" %>
  11. <html>
  12. <head>
  13. <title>$Title$</title>
  14. </head>
  15. <body>
  16. <%
  17. String username="";
  18. String pass="";
  19. String hobby="";
  20. String hobby2="";
  21. Cookie cookie4=DBCookie.getCookie(request.getCookies(),"hobby2");
  22. if(cookie4!=null){
  23. Cookie cookie5=DBCookie.getCookie(request.getCookies(),"hobby");
  24. if(cookie5!=null){
  25. Cookie cookie1= DBCookie.getCookie(request.getCookies(),"username");
  26. if(cookie1!=null){
  27. username=cookie1.getValue();
  28. }
  29. Cookie cookie2=DBCookie.getCookie(request.getCookies(), "pass");
  30. if(cookie2!=null){
  31. pass=cookie2.getValue();
  32. }
  33. hobby=cookie5.getValue();
  34. }
  35. hobby2=cookie4.getValue();
  36. }else{
  37. Cookie cookie3=DBCookie.getCookie(request.getCookies(),"hobby");
  38. if(cookie3!=null){
  39. Cookie cookie1= DBCookie.getCookie(request.getCookies(),"username");
  40. if(cookie1!=null){
  41. username=cookie1.getValue();
  42. }
  43. Cookie cookie2=DBCookie.getCookie(request.getCookies(), "pass");
  44. if(cookie2!=null){
  45. pass=cookie2.getValue();
  46. }
  47. hobby=cookie3.getValue();
  48. }
  49. }
  50. %>
  51. <form action="treeServlet" method="post">
  52. 账号:<input type="text" name="username" value=<%=username%> ><br>
  53. 密码:<input type="text" name="pass" value=<%=pass%> ><br>
  54. <input type="radio" name="hobby" <%=hobby%> value="1">记住密码<br>
  55. <input type="radio" name="hobby2" <%=hobby2%> value="2">自动登录<br>
  56. <input type="submit">
  57. <%
  58. if(hobby2.equals("checked")){
  59. response.sendRedirect("treeServlet?username="+username+"&pass="+pass+"");
  60. }
  61. %>
  62. </form>
  63. </body>
  64. </html>

成功页面success.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2020/10/21
  5. Time: 21:18
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>Title</title>
  12. </head>
  13. <body>
  14. <h2>登录成功</h2>
  15. <form action="index.jsp">
  16. <input type="submit" value="返回主页">
  17. </form>
  18. </body>
  19. </html>

失败页面error.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2020/10/21
  5. Time: 21:19
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>Title</title>
  12. </head>
  13. <body>
  14. <h2>登录失败</h2>
  15. <a href="index.jsp">回主页</a>
  16. </body>
  17. </html>

发表评论

表情:
评论列表 (有 0 条评论,188人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js如何实现登录记住密码

    常见的很多网站登录,都有记住密码功能,下面是用js实现的记住密码功能(代码用的源生js,不用引入任何插件,大家如果引入了jQuery,可以进行修改,优化) js部分