cookie实现登陆页面保存用户名

cookie实现登陆页面保存用户名

大家好,又见面了,我是全栈君。

1.首先是用户名,密码的input 和保存状态的checkbox

[html] 
view plain
 copy

 

  1. <input class=“ipt” type=“text” name=‘username’ value=‘${name}’/>  
  2. <input class=“ipt” type=‘password’ name=‘password’ id=‘password’ />  
  3. <input style=” margin-left:60px; margin-right:10px; ” type=“checkbox” name=“rememberMe” id=“rememberMe” />  

 

没找到怎么保存checkbox的状态,我就自己写了个

 

[html] 
view plain
 copy

 

  1. <script>  
  2.     window.onload = function() {  
  3.         if (‘${name}’ != ”) {  
  4.             document.getElementById(‘rememberMe’).checked = true;  
  5.         } else {  
  6.             document.getElementById(‘rememberMe’).checked = false;  
  7.         }  
  8.     }  
  9. </script>  

 

2.然后在jsp顶部加入java代码,用于读取cookie

[html] 
view plain
 copy

 

  1. <%  
  2.     String name = “”;  
  3.     String psw = “”;  
  4.     String checked = “”;  
  5.     Cookie[] cookies = request.getCookies();  
  6.     if(cookies != null && cookies.length>0){  
  7.         for(int i =0; i<cookies.length; i++){  
  8.             if(cookies[i].getName().equals(“name”)){  
  9.                 name=cookies[i].getValue();  
  10.                 request.setAttribute(“name”,name);  
  11.             }  
  12.                   
  13.             if(cookies[i].getName().equals(“psw”)){  
  14.                 psw=cookies[i].getValue();  
  15.                 request.setAttribute(“psw”,psw);  
  16.             }  
  17.         }  
  18.     }  
  19. %>  

 

3.其次,在登陆到后台验证完毕密码后面加入

[java] 
view plain
 copy

 

  1. //处理Cookie  
  2. addCookie(username , pwd ,response ,request);  

下面是addCookie这个方法

 

[java] 
view plain
 copy

 

  1. /**Cookie的实现     
  2.      * @throws UnsupportedEncodingException **/  
  3.     private void addCookie(String name, String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException  {  
  4.         if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){  
  5.             //创建Cookie  
  6. //          Cookie nameCookie=new Cookie(“name”,URLEncoder.encode(name,”utf-8″));  
  7.             Cookie nameCookie=new Cookie(“name”,name);  
  8.             Cookie pswCookie=new Cookie(“psw”,password);  
  9.               
  10.             //设置Cookie的父路径  
  11.             nameCookie.setPath(request.getContextPath()+“/”);  
  12.             pswCookie.setPath(request.getContextPath()+“/”);  
  13.               
  14.             //获取是否保存Cookie  
  15.             String rememberMe=request.getParameter(“rememberMe”);  
  16.             if(rememberMe==null){
    //不保存Cookie  
  17.                 nameCookie.setMaxAge(0);  
  18.                 pswCookie.setMaxAge(0);  
  19.             }else{
    //保存Cookie的时间长度,单位为秒  
  20.                 nameCookie.setMaxAge(7*24*60*60);  
  21.                 pswCookie.setMaxAge(7*24*60*60);  
  22.             }  
  23.             //加入Cookie到响应头  
  24.             response.addCookie(nameCookie);  
  25.             response.addCookie(pswCookie);  
  26.         }  
  27.     }  
  28. }  

    1.首先是用户名,密码的input 和保存状态的checkbox

    [html] 
    view plain
     copy

     

    1. <input class=“ipt” type=“text” name=‘username’ value=‘${name}’/>  
    2. <input class=“ipt” type=‘password’ name=‘password’ id=‘password’ />  
    3. <input style=” margin-left:60px; margin-right:10px; ” type=“checkbox” name=“rememberMe” id=“rememberMe” />  

     

    没找到怎么保存checkbox的状态,我就自己写了个

     

    [html] 
    view plain
     copy

     

    1. <script>  
    2.     window.onload = function() {  
    3.         if (‘${name}’ != ”) {  
    4.             document.getElementById(‘rememberMe’).checked = true;  
    5.         } else {  
    6.             document.getElementById(‘rememberMe’).checked = false;  
    7.         }  
    8.     }  
    9. </script>  

     

    2.然后在jsp顶部加入java代码,用于读取cookie

    [html] 
    view plain
     copy

     

    1. <%  
    2.     String name = “”;  
    3.     String psw = “”;  
    4.     String checked = “”;  
    5.     Cookie[] cookies = request.getCookies();  
    6.     if(cookies != null && cookies.length>0){  
    7.         for(int i =0; i<cookies.length; i++){  
    8.             if(cookies[i].getName().equals(“name”)){  
    9.                 name=cookies[i].getValue();  
    10.                 request.setAttribute(“name”,name);  
    11.             }  
    12.                   
    13.             if(cookies[i].getName().equals(“psw”)){  
    14.                 psw=cookies[i].getValue();  
    15.                 request.setAttribute(“psw”,psw);  
    16.             }  
    17.         }  
    18.     }  
    19. %>  

     

    3.其次,在登陆到后台验证完毕密码后面加入

    [java] 
    view plain
     copy

     

    1. //处理Cookie  
    2. addCookie(username , pwd ,response ,request);  

    下面是addCookie这个方法

     

    [java] 
    view plain
     copy

     

    1. /**Cookie的实现     
    2.      * @throws UnsupportedEncodingException **/  
    3.     private void addCookie(String name, String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException  {  
    4.         if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){  
    5.             //创建Cookie  
    6. //          Cookie nameCookie=new Cookie(“name”,URLEncoder.encode(name,”utf-8″));  
    7.             Cookie nameCookie=new Cookie(“name”,name);  
    8.             Cookie pswCookie=new Cookie(“psw”,password);  
    9.               
    10.             //设置Cookie的父路径  
    11.             nameCookie.setPath(request.getContextPath()+“/”);  
    12.             pswCookie.setPath(request.getContextPath()+“/”);  
    13.               
    14.             //获取是否保存Cookie  
    15.             String rememberMe=request.getParameter(“rememberMe”);  
    16.             if(rememberMe==null){
      //不保存Cookie  
    17.                 nameCookie.setMaxAge(0);  
    18.                 pswCookie.setMaxAge(0);  
    19.             }else{
      //保存Cookie的时间长度,单位为秒  
    20.                 nameCookie.setMaxAge(7*24*60*60);  
    21.                 pswCookie.setMaxAge(7*24*60*60);  
    22.             }  
    23.             //加入Cookie到响应头  
    24.             response.addCookie(nameCookie);  
    25.             response.addCookie(pswCookie);  
    26.         }  
    27.     }  
    28. }  
    29. 这是我借鉴CSDN一位前辈的

转载于:https://www.cnblogs.com/110lsm/p/8624840.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107625.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 在WIN7系统的笔记本上建立WIFI热点「建议收藏」

    在WIN7系统的笔记本上建立WIFI热点

    2022年1月21日
    85
  • 深入理解Java类加载器(ClassLoader)[通俗易懂]

    深入理解Java类加载器(ClassLoader)[通俗易懂]【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)http://blog.csdn.net/javazejian/article/details/73413292出自【zejian的博客】关联文章:深入理解Java类型信息(Class对象)与反射机制深入理解Java枚举类型(enum)深入理解Java注解类型(@Annotation)深入理解

    2025年9月22日
    6
  • mysql 字符串类型 分区_MySQL分区类型

    mysql 字符串类型 分区_MySQL分区类型博文大纲:1、RANGE分区2、LIST分区3、HASH分区4、key分区5、MySQL分表和分区的区别6、附加:如何实现将分区放在不同的目录下进行存储MySQL分区类型如下:RANFGE分区LIST分区HASH分区key分区上面的四种分区的条件必须是整形,如果不是整形需要通过函数将其转换为整形。1、RANGE分区RANGE分区是基于属于一个给定连续区间的列值,把多行分配给分区。这些区间要连续且不…

    2022年6月8日
    31
  • jar运行发生ClassNotFound的完整解决方案

    jar运行发生ClassNotFound的完整解决方案原文地址:http://www.java2000.net/viewthread.jsp?tid=6053转载请注明上述链接或者CSDN的链接1今天彻底测试了jar程序TestJar.javapackagenet.java2000.test.jar;importjavax.swing.JOptionPane;importorg.springframework.beans.fa

    2022年7月16日
    21
  • 【Python学习 】Python实现的FTP上传和下载功能

    【Python学习 】Python实现的FTP上传和下载功能一、背景最近公司的一些自动化操作需要使用Python来实现FTP的上传和下载功能。因此参考网上的例子,撸了一段代码来实现了该功能,下面做个记录。二、ftplib介绍Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件。Python2.7系列官方文档:https://docs.python.org/2/lib

    2022年6月12日
    59
  • Python3 发票导出XML转Excel[通俗易懂]

    Python3 发票导出XML转Excel[通俗易懂]发票导出xml后转为Excel

    2022年8月22日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号