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.     }
相關文章
相關標籤/搜索