jsp 通过cookie 记住用户名

Java Web中登录时如何记住用户名呢?

具体思路:

登录界面如下:

 在后台(Servlet 或struts 的action)中进行判断,若登录成功则通过HttpServletResponse 添加一个cookie.

在登录的jsp页面中,通过request 获取cookie数组,然后遍历,若找到相应的cookie,则把cookie的value设置到表单的对应文本框中.

 

具体代码如下:

登录的JSP页面中(核心代码):

Html代码   收藏代码
  1. <body>  
  2. <%  
  3. Cookie[] cookies = request.getCookies();  
  4. String username33="";  
  5. if (cookies != null) {  
  6.     for (Cookie c : cookies) {  
  7.         /*if ("password22".equals(c.getName())) {  
  8.             user.setPassword(URLDecoder.decode(c.getValue(), "utf-8"));  
  9.             continue;  
  10.         }*/  
  11.         if ("userEmail".equals(c.getName())) {  
  12.             username33=URLDecoder.decode(c.getValue(), "utf-8");  
  13.             break;  
  14.         }  
  15.     }  
  16. }  
  17. %>  
  18. <script type="text/javascript">  
  19. window.onload=function(){  
  20. var username1='<%=username33 %>';  
  21. //alert("username1:"+username1);  
  22. if(username1){  
  23.     if(username1!='' && username1!=null &&username1!=undefined){  
  24.         document.getElementsByName("user.username")[0].value=username1;  
  25.     }  
  26. }  
  27. }  
  28. </script>  
  29. ...  
  30. </body>  

 后台的action:

Java代码   收藏代码
  1. if (isLogin) {  
  2.               
  3.             //保存用户名(目前是邮箱)  
  4.               
  5.                 String emaiCookieName = "userEmail";  
  6.                 HttpServletRequest request=ServletActionContext.getRequest();  
  7.                 Cookie[] cookies = request.getCookies();  
  8.                 boolean flag = false;  
  9.                 Cookie emailCook = null;  
  10.                 if (cookies != null)  
  11.                 {  
  12.                     System.out.println("cookie 不为空");  
  13.                     for (Cookie c : cookies)  
  14.                     {  
  15.   
  16.                         if (emaiCookieName.equals(c.getName()))  
  17.                         {  
  18.                             System.out.println("找到了 "+emaiCookieName);  
  19.                             System.out.println("cookie的值为 "+c.getValue());  
  20.                             try {  
  21.                                 c.setValue(URLEncoder.encode(this.user.getUsername(), "utf-8"));  
  22.                             } catch (UnsupportedEncodingException e) {  
  23.                                 e.printStackTrace();  
  24.                             }  
  25.                             emailCook = c;  
  26.                             flag = true;  
  27.                             break;  
  28.                         }  
  29.                     }  
  30.                       
  31.                 }  
  32.                 if (!flag)  
  33.                 {  
  34.                     System.out.println("没有找到 "+emaiCookieName);  
  35.   
  36.   
  37.                     try {  
  38.                         emailCook = new Cookie(emaiCookieName, URLEncoder.encode(  
  39.                                 this.user.getUsername(), "utf-8"));  
  40.                     } catch (UnsupportedEncodingException e) {  
  41.                         e.printStackTrace();  
  42.                     }  
  43.                 }  
  44.   
  45.                     if (null != emailCook)  
  46.                     {  
  47.                          HttpServletResponse response=ServletActionContext.getResponse();  
  48.                         if(!ValueWidget.isNullOrEmpty(issave) && issave.equalsIgnoreCase("save")){  
  49.                             emailCook.setMaxAge(10000000);  
  50.                              
  51.                             response.addCookie(emailCook);  
  52.                             System.out.println("保存cookie");  
  53.                          }else{  
  54.                                 System.out.println("让 cookie 失效");  
  55.                                 emailCook.setMaxAge(0);  
  56.                                 response.addCookie(emailCook);  
  57.                          }  
  58.                     }  
  59.   
  60.                      
  61.               
  62.             return Action.SUCCESS;  
  63.         }  

 源代码见附件

说明:

该项目使用maven 构建;

IDE:eclipse

数据库:MySQL

登录地址:http://localhost:8080/shop_goods/user/loginInput.action

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值