Cookie项目---记住用户名

向右看齐 2022-01-26 10:15 344阅读 0赞

check.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11. request.setCharacterEncoding("utf-8");
  12. String name = request.getParameter("uname");
  13. String pwd = request.getParameter("upwd");
  14. Cookie cookie = new Cookie ("name",name);
  15. response.addCookie(cookie);
  16. response.sendRedirect("result.jsp");
  17. %>
  18. </body>
  19. </html>

login.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%!
  11. String uname ;
  12. %>
  13. <%
  14. Cookie[] cookies = request.getCookies();
  15. for(Cookie cookie : cookies)
  16. {
  17. if(cookie.getName().equals("uname"))
  18. {
  19. uname = cookie.getValue();
  20. }
  21. }
  22. %>
  23. <form action="check.jsp" method="post" >
  24. 用户名:<input type="text" name="uname" value="<%=(uname==null?"":uname)%>"> <br/>
  25. 密码:<input type="password" name="upwd"><br/>
  26. <input type="submit" value="登录"><br/>
  27. </form>
  28. </body>
  29. </html>

result.jsp在这里插入图片描述

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 欢迎进入!!!!!!!!!
  11. </body>
  12. </html>

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 git记住用户名和密码

    在使用git时,如果用的是HTTPS的方式,则每次提交,都会让输入用户名和密码,久而久之,就会感觉非常麻烦,那么该如何解决呢? 方式一:使用SSH,添加ssh key。