让cookie失效要注意的问题

让cookie失效要注意的问题

下面的代码无法让cookie 失效,



 

因为保存的cookie 设置了path,

失效时没有设置path, 所以无法cookie 失效,

注意:想让cookie 失效,除了设置有效期为0,其他参数(path,domain)必须完全一样,

正确的地址:




Java代码  收藏代码
  1. /*** 
  2.      * 是否保存cookie 
  3.      * @param response 
  4.      * @param emaiCookieName 
  5.      * @param cookieValue 
  6.      * @param isSave : 是否保存用户名(记住用户名) 
  7.      * @return 
  8.      */  
  9.     public static Cookie rememberMe(Cookie[] cookies , /*HttpServletRequest request,*/HttpServletResponse response,String emaiCookieName, String cookieValue,  
  10.             boolean isSave) {  
  11. //      HttpServletRequest request = ServletActionContext.getRequest();  
  12.   
  13.         boolean flag = false;  
  14.         // Cookie passwordCook = null;  
  15.         Cookie emailCook = null;  
  16.         if (cookies != null) {  
  17.             System.out.println("cookie 不为空");  
  18.             for (Cookie c : cookies) {  
  19.                 // if (passwordCookieName.equals(c.getName()))  
  20.                 // {  
  21.                 // c.setValue(URLEncoder.encode(password, "utf-8"));  
  22.                 // passwordCook = c;  
  23.                 // flag = true;  
  24.                 // continue;  
  25.                 // }  
  26. //              if(c.getName().equals(Constant2.COOKIE_KEY_ISAUTO_LOGIN )){  
  27. //                  System.out.println(Constant2.COOKIE_KEY_ISAUTO_LOGIN+":"+cookieValue);  
  28. //              }  获取地址
  29.                 if (emaiCookieName.equals(c.getName()) ) {  
  30.                     System.out.println("找到了 " + emaiCookieName);  
  31.                     System.out.println("cookie的值为 " + c.getValue());  
  32.                     if((! ValueWidget.isNullOrEmpty(cookieValue))){  
  33.                     try {  
  34.                         c.setValue(URLEncoder.encode(cookieValue, "utf-8"));  
  35.                     } catch (UnsupportedEncodingException e) {  
  36.                         e.printStackTrace();  
  37.                     }  
  38.                     }  
  39.                     emailCook = c;  
  40.                     flag = true;  
  41.                     break;  
  42.                 }  
  43.             }  
  44.   
  45.         }  
  46.   
  47. //      HttpServletResponse response = ServletActionContext.getResponse();  
  48.         if (isSave) {  
  49.             if (!flag) {  
  50.                 System.out.println("没有找到 " + emaiCookieName);  
  51.                 // passwordCook = new Cookie(passwordCookieName, URLEncoder  
  52.                 // .encode(password, "utf-8"));  
  53.                 try {  
  54.                     emailCook = new Cookie(emaiCookieName, URLEncoder.encode(  
  55.                             cookieValue, "utf-8"));  
  56.                 } catch (UnsupportedEncodingException e) {  
  57.                     e.printStackTrace();  
  58.                 }  
  59.             }  
  60.             emailCook.setMaxAge(10000000);//单位是秒,所以大概115 天  
  61.             emailCook.setPath("/");//设置cookie时,设置path为根路径  
  62.             response.addCookie(emailCook);  
  63.             flag=true;  
  64.             System.out.println("保存cookie:"+emailCook.getValue());  
  65.         } else {  
  66.             if (flag) {  
  67.                 System.out.println("让 cookie 失效");  
  68.                 emailCook.setMaxAge(0);  
  69.                 emailCook.setPath("/");//设置cookie时,设置path为根路径 ,如果不设置path ,则无法让cookie 失效  
  70.                 response.addCookie(emailCook);  
  71.             }  
  72.         }  
  73.   
  74.   
  75.         return emailCook;  
  76.     }  
展开阅读全文

没有更多推荐了,返回首页