java cookie 永不过期,从Java设置持久性cookie在IE中不起作用

All,

Although I see related topics on the forum, but I don't see a clear solution on this issue.

I am trying to set a javax.servlet.http.Cookie with an expiration time (so that it persists across browser sessions). Code:

public void respond(HttpServletRequest req, HttpServletResponse resp) {

int expiration = 3600;

Cookie cookie = new Cookie("TestCookie", "xyz");

cookie.setDomain("");

cookie.setVersion(0);

cookie.setPath("/");

cookie.setMaxAge(expiration);

cookie.setSecure(false);

resp.addCookie(cookie);

}

I don't see this cookie being set when I check in IE developer tools. Searching on the internet gave me clues that IE doesn't consider Max-Age, but only works with Expires. If this does not work for IE, then is there a proven way of setting the HTTP response headers for a persistent cookie so that it works for IE?

PS: This works fine on all other browsers.

I tried creating a string for the cookie having expires attribute. IE succeeded in creating it, but it lost the domain (default - "") and showed ".com" and turned it into a session cookie instead of a persistent cookie. This again works fine on all other browsers.

Please help.

Thanks.

解决方案

Working with IE9, I found that it was the HttpOnly attribute that was required in order to get it to echo the cookie value on subsequent posts, e.g:

Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值