在 Java Servlet 中如何更改现有 cookie 的值?
在 Java Servlet 中如何更改现有 cookie 的值? HttpServletResponse中有addCookie方法,但没有deleteCookie或editCookie
In a Java Servlet how can I change the value of an existing cookie? There is an addCookie method, but no deleteCookie or editCookie in HttpServletResponse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那些确实不存在。只需自己创建实用方法即可实现此目的。特别是得到想要的cookie是相当臃肿的。例如,
要编辑 cookie,请设置其值,然后将其添加到响应中:
如有必要,请设置 maxage、路径和域(如果它们与默认值不同)。即客户端不会发回此信息。
要删除 cookie,请将最大期限设置为
0
(最好将值设置为null
):如果需要,请设置路径和域(如果它们与默认值不同) 。即客户端不会发回此信息。
Those indeed don't exist. Just create utility methods yourself which do that. Particularly getting the desired cookie is quite bloated. E.g.
To edit a cookie, set its value and then add it to the response:
Set if necessary the maxage, path and domain as well if they differs from your default. The client namely doesn't send this information back.
To delete a cookie, set the max age to
0
(and preferably also the value tonull
):Set if necessary the path and domain as well if they differs from your default. The client namely doesn't send this information back.
这是来自 kodejava 的一个示例:
}
这将获取 cookie 列表,获取您想要的列表,而不是打印出值,而是执行类似于此的操作:
HTH,
James
Here is an example from kodejava:
}
That will get the cookies list, get the one you want and instead of printing out values, do something similar to this:
HTH,
James