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

来源:互联网 发布:赤焰狂魔莫小贝 知乎 编辑:程序博客网 时间:2024/05/29 02:43

代码如下

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">
  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">
  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">
  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>

 

本文链接:https://liuyanzhao.com/4833.html

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 身份证不能刷了怎么办 身份证指纹错了怎么办 指纹手机丢了怎么办 异地办理临时身份证怎么办 杭州办理外地身份证怎么办 办理身份证没有户口本怎么办 2018身份证掉了怎么办 双户口注销社保怎么办 常用户口被注销怎么办 刚到厦门怎么办身份证 新疆身份证丢了怎么办 技能证书丢了怎么办 16岁拍身份证怎么办 16岁以下怎么办身份证 办身份证要证明怎么办 15岁怎么办银行卡淘宝 满16岁怎么办银行卡 电脑最小化后不见了怎么办 满16周岁怎么办银行卡 给儿童办身份证怎么办 当兵前身份证号码不符怎么办 身份证后六位密码x怎么办 借了贷款人死了怎么办 贷款实在还不起怎么办 个人贷款还不起怎么办 珠海派出所办事不公平怎么办 改身份证后学籍怎么办? 威海身份证丢了怎么办 车牌轻微变形了怎么办 车牌照丢一个怎么办 小车车牌掉了怎么办 车牌撞变形了怎么办 车牌烂了一块怎么办 机动车牌照丢了怎么办 汽车想换车牌怎么办 机动车前牌照丢失怎么办 汽车后牌照丢失怎么办 机动车扣分超证怎么办 联通香港无服务怎么办 长乐改区 闽侯怎么办 学校代码查不到怎么办