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


    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=" 180px;"><input name="checkImg" id="checkImg" type="text" class="input" style=" 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. }  
  • 相关阅读:
    国产html编辑器
    豆瓣90mhz电台还行
    购物搜索
    linode vps 优化集锦
    这个系统,我看行,
    代理网站
    合理设置域名TTL值给网站加速
    dns切换到dnspod
    elixir usage
    JSSH介绍
  • 原文地址:https://www.cnblogs.com/zmwf/p/9097868.html
Copyright © 2020-2023  润新知