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

avatar 2017年07月02日20:46:40 1 2109 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

代码如下


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
  • 微信
  • 交流学习,服务定制
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0