从 Java 设置持久 cookie 在 IE 中不起作用
所有,
虽然我在论坛上看到了相关主题,但我没有看到这个问题的明确解决方案。 我正在尝试设置 javax.servlet.http.Cookie 的过期时间(以便它在浏览器会话中持续存在)。代码:
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);
}
当我签入 IE 开发者工具时,我没有看到此 cookie 被设置。在互联网上搜索给我的线索是IE不考虑Max-Age,而只适用于Expires。如果这对 IE 不起作用,那么是否有一种行之有效的方法来设置持久 cookie 的 HTTP 响应标头,以便它适用于 IE?
PS:这在所有其他浏览器上都可以正常工作。
我尝试为具有过期属性的 cookie 创建一个字符串。 IE 成功创建了它,但它丢失了域(默认 - “”)并显示“.com”,并将其转换为会话 cookie 而不是持久 cookie。这在所有其他浏览器上再次运行良好。
请帮忙。 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 IE9,我发现需要
HttpOnly
属性才能让它在后续帖子中回显 cookie 值,例如: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:答案就在 来自 IE 中 servlet 的持久 cookie。
您的情况可能是同一问题的不同形式:也就是说,通过在域前添加“.”前缀。 (我很确定这是版本 1 的 cookie 功能),Java 堆栈中的某些内容决定它是版本 1 的 cookie(IE 无法识别且不保留,甚至是 IE8)并发送该 cookie 格式。
或者,正如该答案所暗示的那样,您的 cookie 值中是否包含无法识别的字符?
The answer is at Persistent cookies from a servlet in IE.
Your case may be a different flavour of the same issue: that is, by prefixing the domain with a "." (which I'm pretty sure is a version 1 cookie feature), something in the Java stack decides it's a version 1 cookie (unrecognized and not persisted by IE, even IE8) and sends that cookie format.
Or, as that answer suggests, is there something in your cookie value that contains an unrecognized character?
由于
javax.servlet.http.Cookie
不允许您为cookie设置Expires
属性,因此您应该手动设置它。您还需要知道
Expires
必须按照 RFC-2616 以Wdy, DD Mon YYYY HH:MM:SS GMT
的形式指定完整日期部分(更多信息)。在Java中你可以这样做:
As
javax.servlet.http.Cookie
does not allow you to setExpires
attribute to the cookie, you should set it manually.You also need to know that
Expires
must be specified in the form ofWdy, DD Mon YYYY HH:MM:SS GMT
following RFC-2616 Full Date section (more info).In Java you can do it this way: