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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Redis和MongoDB的区别(面试受用)

    Redis和MongoDB的区别(面试受用)

    2022年2月20日
  • 接私活必备的10个开源项目(it接私活)

    点击蓝色“GitHubDaily”关注我加个“星标”,每天下午18:35,带你逛GitHub!作者|SevDot来源|http://1t.click/VE8W…

    2022年4月11日
  • 走近webpack(3)–图片的处理[通俗易懂]

    上一章,咱们学了如何用webpack来打包css,压缩js等。这一篇文章咱们来学习一下如何用webpack来处理图片。废话不多说,咱们开始吧。首先,咱们随便找一张你喜欢的图片放到src/images

    2022年3月25日
  • springboot整合了哪些框架_框架java

    springboot整合了哪些框架_框架java目录1.SpringBoot框架说明1.1添加pom文件依赖1.2主启动类2.SSM框架整合2.2添加依赖2.3编辑UserController2.4编辑UserService接口及实现类2.5编辑UserMapper接口及Mapper映射文件2.7Mapper注入报错解决方案​​​​​​​1.SpringBoot框架说明理解:传统框架在使用时,特别的繁琐Spring/SpringMVC/Mybatis在使用时需要……

    2022年8月20日
  • SQLldr_sqlldr远程数据库

    SQLldr_sqlldr远程数据库1.SQLLDR导入 1.1简介 SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件(数据)迁移到ORACLE数据库中。SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PARALLEL)。 2.2语法和参数语法:SQLLDRkeyword=value[,keyword=value,..

    2022年10月25日
  • modbus协议讲解及实现_MODBUS功能码

    modbus协议讲解及实现_MODBUS功能码Modbus是一种单主站的主/从通信模式。Modbus网络上只能有一个主站存在,主站在Modbus网络上没有地址,从站的地址范围为0-247,其中0为广播地址,从站的实际地址范围为1-247。Modbus通信标准协议可以通过各种传输方式传播,如RS232C、RS485、光纤、无线电等。Modbus具有两种串行传输模式,ASCII和RTU。它们定义了数据如何打包、解码的…

    2022年10月21日

发表回复

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

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