做了一个自动登录的页面,可是注销之后cookie就是删除不了,头痛。贴出代码,希望大家可以解释下,我用setMaxAge(0)根本不起作用//设置自动登录时候登录的代码
if(autoLogin.equals("yes")){
int seconds= 14*24*60*60;
Cookie cookie = new Cookie("jclick",username+"=="+password);
cookie.setMaxAge(seconds);
cookie.setPath("/");
response.addCookie(cookie);
}
//struts拦截器中的代码
     if(cookies != null){
     for (Cookie cookie : cookies){
     if("jclick".equals(cookie.getName())){
     String[] cookieValue = cookie.getValue().split("==");
     username = cookieValue[0];
     password = cookieValue[1];
     System.out.println(username+"==cookieAge=="+cookie.getMaxAge());
     }
     }
     }
//注销时候的代码
Cookie cookie = new Cookie("jclick",null);
cookie.setPath("/");
cookie.setMaxAge(1);
response.addCookie(cookie);
System.out.println("delete success!!");
actionContext.getSession().remove("username");
actionContext.getSession().clear();
return SUCCESS;

解决方案 »

  1.   

    cookie.setMaxAge(0); 浏览器关闭时删除
    cookie.setMaxAge(-1); 负数的时候删除当前cookie
      

  2.   

    应该是
    cookie.setMaxAge(0);//立即删除cookie
    cookie.setMaxAge(-1);//浏览器关闭时删除cookie
      

  3.   

    你是正解,api的帮助文档setMaxAgepublic void setMaxAge(int expiry)
    Sets the maximum age of the cookie in seconds.
    A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.Parameters:
    expiry - an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie
    See Also:
    getMaxAge()
      

  4.   

    你注销时清除cookiescookie.setMaxAge(0);
      

  5.   

    楼主你的注销代码
    //注销时候的代码
            Cookie cookie = new Cookie("jclick",null);
            cookie.setPath("/");
            cookie.setMaxAge(1);//设置的是1 cookie.setMaxAge(0);
       
      

  6.   


    这只是测试的。。实验证明。我用cookie.setMaxAge(0);还是没效果代码中的setMaxAge(1)是我测试用的。