java web项目用cookie记住用户名、密码

本文介绍了一个使用JSP实现的登录页面案例,详细展示了如何通过Cookie存储用户登录信息以便下次访问时自动填充用户名和密码。此外,还介绍了如何根据用户选择设置持久化的登录状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.jsp页面:

 


 
  1. <% String flag = (String)session.getAttribute("flag")==null?"":(String)session.getAttribute("flag");  
  2.    
  3. String name = "";  
  4. String password = "";  
  5. try{   
  6.     Cookie[] cookies=request.getCookies();   
  7.     if(cookies!=null){   
  8.     for(int i=0;i<cookies.length;i++){   
  9.         if(cookies[i].getName().equals("cookie_user")){   
  10.         String value =  cookies[i].getValue();  
  11.         if(value!=null&&!"".equals(value)){  
  12.             name=cookies[i].getValue().split("-")[0];   
  13.             if(cookies[i].getValue().split("-")[1]!=null && !cookies[i].getValue().split("-")[1].equals("null")){  
  14.      password=cookies[i].getValue().split("-")[1];   
  15.              }  
  16.                         
  17.             }  
  18.             }   
  19.         request.setAttribute("name",name);   
  20.         request.setAttribute("passward",password);   
  21.     }   
  22.     }   
  23. }catch(Exception e){   
  24.     e.printStackTrace();   
  25. }   
  26. %>   
  27. <body>  
  28. <div id="logo"><img src="<%=request.getContextPath() %>/frontsite/Images/Logo.jpg" width="244" height="44" alt="" /></div>  
  29. <div id="loginfrm">  
  30. <form action="<%=request.getContextPath() %>/frontsite/login.do?method=login" method="post" id="forms" onsubmit="checkForm();return false;">  
  31.     <div class="frm">  
  32.         <ul>  
  33.             <li class="l1">用户名:</li>  
  34.             <li class="l2"><input name="login_id" type="text" id="login_id" class="input" value="<%=name %>"/></li>  
  35.             <li class="l3">* 最大限度为20字节</li>  
  36.             <li class="l1">密码:</li>  
  37.             <li class="l2"><input name="login_pwd" type="password" id="login_pwd" class="input" value="<%=password %>"/></li>  
  38.             <li class="l3">* 最大限度为20字节</li>  
  39.                
  40.              <li class="l1">验证码:  
  41.                 <li class="l2" style="width: 180px;"><input name="checkImg" id="checkImg" type="text" class="input" style="width: 60px;" size="10" />  
  42.                   <img src="<%=request.getContextPath() %>/CheckImg_FT" width="49" height="22" /></li>  
  43.                
  44.             <li class="l4">  
  45.                 <label><input type="checkbox" name="flag" id="flag" value="1" <%if(flag!=null && flag.equals("1")){%> checked ; value ="1"; <%}else {%value="0" <%;}%/>记住密码</label>  
  46.                 <label><span style="margin-left:10px; color: #F00;" ><html:errors /></span></label>                 
  47.             </li>  
  48.    
  49.             <li class="l4"><input type="image" name="imageField" id="imageField" src="<%=request.getContextPath() %>/frontsite/Images/login_bnt.jpg" /></li>  
  50.         </ul>  
  51.     </div>  
  52. </form>  
  53. </div>  
  54. </body>  


2.java 类:

 


 
    1. String flag = request.getParameter("flag");  
    2. //set cookie  
    3. if(flag!=null && flag.equals("1")){  
    4.     Cookie cookie = new Cookie("cookie_user", po.getLogin_id()+"-"+form.getLogin_pwd());                  
    5.     cookie.setMaxAge(60*60*24*30); //cookie 保存30天  
    6. response.addCookie(cookie);  
    7. }else{    
    8.     Cookie cookie = new Cookie("cookie_user",po.getLogin_id()+"-"+null);                  
    9.     cookie.setMaxAge(60*60*24*30); //cookie 保存30天  
    10.     response.addCookie(cookie);               
    11. }  

转载于:https://www.cnblogs.com/zmwf/p/9097868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值