[关闭]
@nalan90 2018-06-29T11:16:18.000000Z 字数 2539 阅读 510

四、JSP状态管理

JAVAEE开发


内容列表

# 保存用户状态的两大机制

JSP中创建与使用Cookie
  1. Cookie cookie = new Cookie(String key, Object value);
  1. response.addCookie(cookie);
  1. Cookie[] cookies = request.getCookies();
  1. ## login.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  3. <html>
  4. <head>
  5. <title>登录页面</title>
  6. </head>
  7. <body>
  8. <%
  9. String username = "";
  10. String password = "";
  11. Cookie[] cookies = request.getCookies();
  12. if (cookies != null && cookies.length > 0) {
  13. for (Cookie c: cookies) {
  14. if (c.getName().equals("username")) {
  15. username = URLDecoder.decode(c.getValue(),"utf-8");
  16. }
  17. if (c.getName().equals("password")) {
  18. password = c.getValue();
  19. }
  20. }
  21. }
  22. %>
  23. <h1>用户登录</h1>
  24. <hr>
  25. <form action="dologin.jsp" name="loginForm" method="post">
  26. 用户名: <input type="text" name="username" value="<%=username%>"><br>
  27. 密码:<input type="password" name="password" value="<%=password%>"><br>
  28. <input type="checkbox" name="isUseCookie" checked="checked">十天内记住我的登录状态<br>
  29. <input type="submit" value="login">
  30. </form>
  31. </body>
  32. </html>
  33. ## dologin.jsp
  34. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  35. <%
  36. request.setCharacterEncoding("utf-8");
  37. %>
  38. <%
  39. String[] isUseCookies = request.getParameterValues("isUseCookie");
  40. if (isUseCookies != null && isUseCookies.length > 0) {
  41. String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
  42. String password = request.getParameter("password");
  43. Cookie nameCookie = new Cookie("username", username);
  44. Cookie passwordCookie = new Cookie("password", password);
  45. nameCookie.setMaxAge(600);
  46. passwordCookie.setMaxAge(600);
  47. response.addCookie(nameCookie);
  48. response.addCookie(passwordCookie);
  49. } else {
  50. Cookie[] cookies = request.getCookies();
  51. if (cookies != null && cookies.length > 0) {
  52. for (Cookie c: cookies) {
  53. if (c.getName().equals("username") || c.getName().equals("password")) {
  54. c.setMaxAge(0);
  55. response.addCookie(c);
  56. }
  57. }
  58. }
  59. }
  60. %>
  61. <a href="users.jsp">查看用户信息</a>
  62. ## users.jsp
  63. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  64. <html>
  65. <head>
  66. <title>Title</title>
  67. </head>
  68. <body>
  69. <%
  70. String username = "";
  71. String password = "";
  72. Cookie[] cookies = request.getCookies();
  73. if (cookies != null && cookies.length > 0) {
  74. for (Cookie c: cookies) {
  75. if (c.getName().equals("username")) {
  76. username = URLDecoder.decode(c.getValue(),"utf-8");
  77. }
  78. if (c.getName().equals("password")) {
  79. password = c.getValue();
  80. }
  81. }
  82. }
  83. %>
  84. <br>
  85. 用户名: <%=username%><br>
  86. 密码: <%=password%>
  87. </body>
  88. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注