jsp 经过cookie 记住用户名

jsp 经过cookie 记住用户名

记住用户名记住密码remember mecookie会话javascript

java web中登陆时如何记住用户名呢?java

具体思路:mysql

登陆界面以下:

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

在登陆的jsp页面中,经过request 获取cookie数组,而后遍历,若找到相应的cookie,则把cookie的value设置到表单的对应文本框中.objective-c

 

具体代码以下:sql

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

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

 

 

1 

1 

分享到:  

java 获取文件大小 | javascript 编程要注意的问题

参考知识库

Hbase知识库3874  关注 | 63  收录

MongoDB知识库3739  关注 | 271  收录

区块链知识库2484  关注 | 90  收录

Objective-C知识库3703  关注 | 1209  收录

评论

 

4 楼 hw1287789687 2014-10-07  

应用场景:

Java代码  收藏代码

  1. /*** 
  2.      *  
  3.      * @param model 
  4.      * @param user 
  5.      * @param request 
  6.      * @param session 
  7.      * @return  : 返回null,则登陆成功,<br>不然,登陆失败 
  8.      */  
  9.     public String loginCommon(Model model,User user,HttpServletRequest request,HttpServletResponse response  
  10.             , HttpSession session,String issave){  
  11.         if(ValueWidget.isNullOrEmpty(user)||ValueWidget.isNullOrEmpty(user.getUsername())){  
  12. //          model.addAttribute("info", "请输入用户名.");  
  13. //          System.out.println("user is null");  
  14. //          return "user/login";  
  15.             return "请输入用户名.";  
  16.         }  
  17.         if(user.getUsername().trim().length()<3 || user.getUsername().trim().length()>16){  
  18. //          model.addAttribute("info", "请输入3-16位用户名字符.");  
  19. //          return "user/login";  
  20.             return "请输入3-16位用户名字符.";  
  21.         }  
  22.         User user1 = userDao.getByUsername(user.getUsername());  
  23.         if(user1==null){  
  24. //          model.addAttribute("info", "您输入的用户名不存在.");  
  25. //          return "user/login";  
  26.             return "您输入的用户名不存在.";  
  27.         }  
  28.         if(!user.getPassword().equals(user1.getPassword())){  
  29. //          model.addAttribute("info", "您输入的密码有误.");  
  30. //          return "user/login";  
  31.             return "您输入的密码有误.";  
  32.         }  
  33.         session.setAttribute(Constant2.SESSION_KEY_LOGINED_USER, user1);//登陆成功的标识有两个:"user",Constant2.SESSION_KEY_LOGINED_FLAG  
  34.         session.setAttribute(Constant2.SESSION_KEY_LOGINED_FLAG, Constant2.FLAG_LOGIN_SUCCESS);//登陆成功的标识有两个:"user",Constant2.SESSION_KEY_LOGINED_FLAG  
  35.         model.addAttribute("user", user1);  
  36.         boolean isSave = !ValueWidget.isNullOrEmpty(issave)  
  37.                 && issave.equalsIgnoreCase("save");  
  38.         System.out.println("isSave:"+isSave);  
  39.         WebServletUtil.rememberMe(request,response,"userEmail", user.getUsername(), isSave);  
  40.         return null;  
  41.     }  

3 楼 hw1287789687 2014-10-07  

hw1287789687 写道

封装的方法

Java代码  收藏代码

  1. /*** 
  2.      * 是否保存cookie 
  3.      * @param request 
  4.      * @param response 
  5.      * @param emaiCookieName 
  6.      * @param cookieValue 
  7.      * @param isSave : 是否保存用户名(记住用户名) 
  8.      * @return 
  9.      */  
  10.     public static Cookie rememberMe(HttpServletRequest request,HttpServletResponse response,String emaiCookieName, String cookieValue,  
  11.             boolean isSave) {  
  12. //      HttpServletRequest request = ServletActionContext.getRequest();  
  13.         Cookie[] cookies = request.getCookies();  
  14.         boolean flag = false;  
  15.         // Cookie passwordCook = null;  
  16.         Cookie emailCook = null;  
  17.         if (cookies != null) {  
  18.             System.out.println("cookie 不为空");  
  19.             for (Cookie c : cookies) {  
  20.                 // if (passwordCookieName.equals(c.getName()))  
  21.                 // {  
  22.                 // c.setValue(URLEncoder.encode(password, "utf-8"));  
  23.                 // passwordCook = c;  
  24.                 // flag = true;  
  25.                 // continue;  
  26.                 // }  
  27.                 if (emaiCookieName.equals(c.getName()) &&(! ValueWidget.isNullOrEmpty(cookieValue))) {  
  28.                     System.out.println("找到了 " + emaiCookieName);  
  29.                     System.out.println("cookie的值为 " + c.getValue());  
  30.                     try {  
  31.                         c.setValue(URLEncoder.encode(cookieValue, "utf-8"));  
  32.                     } catch (UnsupportedEncodingException e) {  
  33.                         e.printStackTrace();  
  34.                     }  
  35.                     emailCook = c;  
  36.                     flag = true;  
  37.                     break;  
  38.                 }  
  39.             }  
  40.   
  41.         }  
  42.   
  43. //      HttpServletResponse response = ServletActionContext.getResponse();  
  44.         if (isSave) {  
  45.             if (!flag) {  
  46.                 System.out.println("没有找到 " + emaiCookieName);  
  47.                 // passwordCook = new Cookie(passwordCookieName, URLEncoder  
  48.                 // .encode(password, "utf-8"));  
  49.                 try {  
  50.                     emailCook = new Cookie(emaiCookieName, URLEncoder.encode(  
  51.                             cookieValue, "utf-8"));  
  52.                 } catch (UnsupportedEncodingException e) {  
  53.                     e.printStackTrace();  
  54.                 }  
  55.             }  
  56.             emailCook.setMaxAge(10000000);  
  57.             response.addCookie(emailCook);  
  58.             flag=true;  
  59.             System.out.println("保存cookie:"+emailCook.getValue());  
  60.         } else {  
  61.             if (flag) {  
  62.                 System.out.println("让 cookie 失效");  
  63.                 emailCook.setMaxAge(0);  
  64.                 response.addCookie(emailCook);  
  65.             }  
  66.         }  
  67.       
  68.   
  69.         return emailCook;  
  70.     }  



使用场景:

Java代码  收藏代码

  1. if(! StringUtil.isNullOrEmpty(resultCode)&&resultCode.equals("0001")){  
  2.                       
  3.                     System.out.println("issave:"+issave);  
  4.                     boolean isSave = !StringUtil.isNullOrEmpty(issave)  
  5.                             && issave.equalsIgnoreCase("save");  
  6.                     System.out.println("isSave:"+isSave);  
  7.                     Json2Util.rememberMe(emaiCookieName, email, isSave);  
  8.                     success=true;  
  9.                 }  

2 楼 hw1287789687 2014-10-07  

http://hw1287789687.iteye.com/blog/2124945

1 楼 hw1287789687 2014-10-07  

封装的方法

Java代码  收藏代码

  1. /*** 
  2.      * 是否保存cookie 
  3.      * @param request 
  4.      * @param response 
  5.      * @param emaiCookieName 
  6.      * @param cookieValue 
  7.      * @param isSave : 是否保存用户名(记住用户名) 
  8.      * @return 
  9.      */  
  10.     public static Cookie rememberMe(HttpServletRequest request,HttpServletResponse response,String emaiCookieName, String cookieValue,  
  11.             boolean isSave) {  
  12. //      HttpServletRequest request = ServletActionContext.getRequest();  
  13.         Cookie[] cookies = request.getCookies();  
  14.         boolean flag = false;  
  15.         // Cookie passwordCook = null;  
  16.         Cookie emailCook = null;  
  17.         if (cookies != null) {  
  18.             System.out.println("cookie 不为空");  
  19.             for (Cookie c : cookies) {  
  20.                 // if (passwordCookieName.equals(c.getName()))  
  21.                 // {  
  22.                 // c.setValue(URLEncoder.encode(password, "utf-8"));  
  23.                 // passwordCook = c;  
  24.                 // flag = true;  
  25.                 // continue;  
  26.                 // }  
  27.                 if (emaiCookieName.equals(c.getName()) &&(! ValueWidget.isNullOrEmpty(cookieValue))) {  
  28.                     System.out.println("找到了 " + emaiCookieName);  
  29.                     System.out.println("cookie的值为 " + c.getValue());  
  30.                     try {  
  31.                         c.setValue(URLEncoder.encode(cookieValue, "utf-8"));  
  32.                     } catch (UnsupportedEncodingException e) {  
  33.                         e.printStackTrace();  
  34.                     }  
  35.                     emailCook = c;  
  36.                     flag = true;  
  37.                     break;  
  38.                 }  
  39.             }  
  40.   
  41.         }  
  42.   
  43. //      HttpServletResponse response = ServletActionContext.getResponse();  
  44.         if (isSave) {  
  45.             if (!flag) {  
  46.                 System.out.println("没有找到 " + emaiCookieName);  
  47.                 // passwordCook = new Cookie(passwordCookieName, URLEncoder  
  48.                 // .encode(password, "utf-8"));  
  49.                 try {  
  50.                     emailCook = new Cookie(emaiCookieName, URLEncoder.encode(  
  51.                             cookieValue, "utf-8"));  
  52.                 } catch (UnsupportedEncodingException e) {  
  53.                     e.printStackTrace();  
  54.                 }  
  55.             }  
  56.             emailCook.setMaxAge(10000000);  
  57.             response.addCookie(emailCook);  
  58.             flag=true;  
  59.             System.out.println("保存cookie:"+emailCook.getValue());  
  60.         } else {  
  61.             if (flag) {  
  62.                 System.out.println("让 cookie 失效");  
  63.                 emailCook.setMaxAge(0);  
  64.                 response.addCookie(emailCook);  
  65.             }  
  66.         }  
  67.       
  68.   
  69.         return emailCook;  
  70.     }
相关文章
相关标签/搜索