• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

JSP中Cookie在登录功能中的简单应用

后端 Nanait 12年前 (2012-09-01) 852次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

代码如下

login.jsp

  1. <%@page import=“java.net.URLDecoder”%>
  2. <%@ page language=“java” contentType=“text/html; charset=UTF-8”
  3.     pageEncoding=“UTF-8”%>
  4. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd&#8221;>
  5. <html>
  6. <head>
  7. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <h1>用户登录</h1>
  12. <%
  13.     String username = “”;
  14.     String password = “”;
  15.     Cookie[] cookies = request.getCookies();
  16.     if(cookies!=null &&cookies.length>0) {
  17.         for(Cookie c:cookies) {
  18.             if(c.getName().equals(“username”)) {
  19.                 username = URLDecoder.decode(c.getValue(),”utf-8″);//解码
  20.             }
  21.             if(c.getName().equals(“password”)) {
  22.                 password = URLDecoder.decode(c.getValue(),”utf-8″);
  23.             }
  24.         }
  25.     }
  26. %>
  27.     <form action=“doLogin.jsp” method=“post”>
  28.         <table>
  29.             <tr>
  30.                 <td>用户名</td>
  31.                 <td><input type=“text” name=“username” value=“<%=username %>”/></td>
  32.             </tr>
  33.             <tr>
  34.                 <td>密码</td>
  35.                 <td><input type=“password” name=“password” value=“<%=password %>”/></td>
  36.             </tr>
  37.             <tr>
  38.                 <td colspan=“2”>
  39.                 <input type=“checkbox” name=“isUseCookie” checked=“checked”/>10 天记住登录
  40.                 </td>
  41.             </tr>
  42.             <tr>
  43.                 <td clospan=“2”>
  44.                     <input type=“submit” value=“登录”/>
  45.                 </td>
  46.             </tr>
  47.         </table>
  48.     </form>
  49. </body>
  50. </html>

doLogin.jsp

  1. <%@page import=“java.net.URLEncoder”%>
  2. <%@ page language=“java” contentType=“text/html; charset=UTF-8”
  3.     pageEncoding=“UTF-8”%>
  4. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd&#8221;>
  5. <html>
  6. <head>
  7. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11.     <h1>登录成功</h1>
  12.     <br />
  13.     <%
  14.         request.setCharacterEncoding(“utf-8”);
  15.         //首先判断用户是否选择了记住登录选项
  16.         String[] isUseCookies = request.getParameterValues(“isUseCookie”);
  17.         if(isUseCookies!=null &&isUseCookies.length>0) {
  18.             //把用户名和密码保存在 Cookie 里面
  19.             //使用 URLEncoder 解决无法在 Cookie 中保存中文字符串问题
  20.             String username = URLEncoder.encode(request.getParameter(“username”),”utf-8″);
  21.             String password = URLEncoder.encode(request.getParameter(“password”),”utf-8″);
  22.             Cookie usernameCookie = new Cookie(“username”,username);
  23.             Cookie passwordCookie = new Cookie(“password”,password);
  24.             response.addCookie(usernameCookie);
  25.             response.addCookie(passwordCookie);
  26.             usernameCookie.setMaxAge(864000);//设置最大生存期限为 10 天
  27.             passwordCookie.setMaxAge(864000);
  28.         } else {
  29.             Cookie[] cookies = request.getCookies();
  30.             if(cookies!=null && cookies.length>0) {
  31.                 for(Cookie c:cookies) {
  32.                     if(c.getName().equals(“username”)||c.getName().equals(“password”)) {
  33.                         c.setMaxAge(0);//设置 Cookie 失效
  34.                         response.addCookie(c);//重新保存
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     %>
  40.     <a href=“users.jsp” target=“blank”>查看用户信息</a>
  41. </body>
  42. </html>

users.jsp

  1. <%@page import=“java.net.URLDecoder”%>
  2. <%@page import=“org.apache.tomcat.util.http.Cookies”%>
  3. <%@ page language=“java” contentType=“text/html; charset=UTF-8”
  4.     pageEncoding=“UTF-8”%>
  5. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd&#8221;>
  6. <html>
  7. <head>
  8. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
  9. <title>Insert title here</title>
  10. </head>
  11. <body>
  12. <h1>用户信息</h1>
  13. <hr />
  14. <%
  15.     String username = “”;
  16.     String password = “”;
  17.     Cookie[] cookies = request.getCookies();
  18.     if(cookies!=null &&cookies.length>0) {
  19.         for(Cookie c:cookies) {
  20.             if(c.getName().equals(“username”)) {
  21.                 username = URLDecoder.decode(c.getValue(),”utf-8″);//解码
  22.             }
  23.             if(c.getName().equals(“password”)) {
  24.                 password = URLDecoder.decode(c.getValue(),”utf-8″);
  25.             }
  26.         }
  27.     }
  28. %>
  29.     用户名:<%=username %> <br />
  30.     密码:  <%=password %> <br />
  31. </body>
  32. </html>

何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JSP 中 Cookie 在登录功能中的简单应用
喜欢 (0)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!