Cookie记住登陆账号和密码
Cookie作用:简化用户登陆(记住密码),购物车,浏览记录等等
1:jsp中创建与使用cookie 创建Cookie对象 Cookie newCookie=new Cookie(String key,Object value);
2:写入Cookie对象 response.addCookie(newCookie);
3:读取Cookie对象 Cookie[]cookies=request.getCookies();
Cookie实现记忆用户名和密码的功能例子
------------------------login界面-----------------------
<% String username=""; String password=""; Cookie[] c=request.getCookies(); if(c!=null&&c.length>0) { for(Cookie c1:c) { if(c1.getName().equals("username")) { username=URLDecoder.decode(c1.getValue(),"utf-8"); } if(c1.getName().equals("password")) { password=URLDecoder.decode(c1.getValue(),"utf-8"); } } } %> <div class="login"> <form action="dologin.jsp" method="post"> <tr> <td>username</td> <td><input type="text" name="username" value="<%=username %>"></td> </tr> <br> <tr> <td>password</td> <td><input type="password" name="password" value="<%=password%>"></td> </tr> <br> <tr> <td colspan="2" align="center"><input type="checkbox" name="rember" checked="checked">记住密码</td> </tr> <br> <tr> <td colspan=2 align="center"><input type="submit" value="login"> </td> </tr> </form> </div>
------------------------dologin.jsp页面------------------------------
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'dologin.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录成功 <% //首先判断用户是否点击了checkbox记住登录状态 String []s= request.getParameterValues("rember"); if(s!=null&&s.length>0) { //保持用户名和密码保存在cookies中 //String username=request.getParameter("username"); //使用URLEncoder解决无法在cookie中保持中文的问题,先转码,在解码 String username= URLEncoder.encode(request.getParameter("username"), "utf-8"); //String password=request.getParameter("password"); String password= URLEncoder.encode(request.getParameter("password"), "utf-8"); Cookie usernamecookie=new Cookie("username",username); Cookie passwordcookie=new Cookie("password",password); usernamecookie.setMaxAge(86400);//设置最长生存时间为10天 response.addCookie(usernamecookie); response.addCookie(passwordcookie); } %> <a href="user.jsp">查看用户信息</a> </body> </html>
------------------------Cookie读取用户信息页面-------------------------------------
<h1>用户信息</h1> <% String username=""; String password=""; Cookie[] c=request.getCookies(); if(c!=null&&c.length>0) { for(Cookie c1:c) { if(c1.getName().equals("username")) { username=URLDecoder.decode(c1.getValue(),"utf-8"); } if(c1.getName().equals("password")) { password=URLDecoder.decode(c1.getValue(),"utf-8"); } } } %> 用户名:<%=username %> 密码:<%=password %>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步