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


相关推荐

  • Buck的振铃实验与分析

    Buck的振铃实验与分析上上期我们提到了buck电路的开关的振铃波形,本质原因是LC的阻尼振荡。文章偏理论,那BUCK到底是怎么产生尖峰振荡呢?要想把这个问题搞清楚,也很是不容易,所以文章有点长,请直接点赞转发加收藏。问题本期主要分析以下这两个问题:1、死区时间是什么?这里有个小台阶是什么情况?2、上下尖峰振荡是如何产生的?跟哪些因素有关?理想的BUCK的SW波形我们由浅入深,一步一步来,先看理想的开关SW波形—没有尖峰电压的波形。为了能更好的看buck电路各个点的电压电流情况.

    2025年8月10日
    3
  • 如何用matlab编写分段函数_matlab 分段函数

    如何用matlab编写分段函数_matlab 分段函数f(x)的定义如下:2226,04()56,010,231,xxxxfxxxxxxxx且且其它1、写一个函数文件f(x)实现该函数,要求参数x可以是向量;2、作出该函数的图形;3、求出f(x)的零点与最值。解:(1)、编写M函数文件functiony=f(x)n=length(x);ifx<0&x~=-4y=x.^2+x-6;elseif…

    2022年6月14日
    127
  • pig中使用的一些实例语法

    pig中使用的一些实例语法

    2021年12月15日
    52
  • C语言经典面试题_c语言常见面试题

    C语言经典面试题_c语言常见面试题100道c语言面试题2018年01月12日18:52:35cocos+unity+服务器阅读数:9166https://blog.csdn.net/themagickeyjianan/article/details/79047242题目来源:1、中兴、华为、慧通、英华达、微软亚洲技术中心等中外企业面试题目;2、C语言面试宝典(林锐《高质量编程第三版》)。说明:1、部…

    2022年8月28日
    2
  • js面试笔试–prototype详解

    js面试笔试–prototype详解对JavaScript中原型模式的理解一:什么是原型对象?有什么优点?   简单的来说,无论何时,我们创建的每一个函数都有一个prototype属性,这个属性是一个指针,指向一个对象,这个对象包含了通过调用该构造函数所创建的对象共享的属性和方法。其实我们平常的叫法就是指:prototype就是通过该构造函数创建的某个实例的原型对象,但是其实prototype是每个构造函数的属性而已,只能说…

    2022年7月22日
    9
  • mysql 练习题

    mysql 练习题一、上机内容使用SQL语句创建数据库studentsdb。、Createdatabasestudentdb;2.使用SQL语句选择studentsdb为当前使用数据库。Usestudentdb;3.使用SQL语句在studentsdb数据库创建数据表student_info、curriculum、grade,三个表的数据结构如表1-表3所示。createtablestude…

    2022年9月18日
    3

发表回复

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

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