jsp 通过cookie 记住用户名

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

具体思路:

登录界面如下:
jsp 通过cookie 记住用户名
 在后台(Servlet 或struts 的action)中进行判断,若登录成功则通过HttpServletResponse 添加一个cookie.

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

 

具体代码如下:

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

Html代码  jsp 通过cookie 记住用户名
  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代码  jsp 通过cookie 记住用户名
  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

上一篇:使用Configuration Manager部署操作系统(1)


下一篇:C# string格式的日期时间字符串转为DateTime类型