有没有办法从Java中的响应对象读取cookie?
HttpServletResponse 似乎没有公开任何方法来执行此操作。
现在,我正在将一堆日志记录代码添加到一个粗糙且难以理解的 servlet 中,试图弄清楚它到底做了什么。我知道它会设置一堆cookie,但我不知道何时、为什么或什么。如果在 servlet 执行结束时将所有 cookie 记录在 HttpServletResponse 对象中,那就太好了。
我知道 cookie 通常是浏览器的责任,而且我记得在 .NET 中没有办法做到这一点。只是希望 Java 可能有所不同......
但如果这是不可能的 - 关于如何完成我想做的事情还有其他想法吗?
谢谢,一如既往。
It doesn't seem that HttpServletResponse exposes any methods to do this.
Right now, I'm adding a bunch of logging code to a crufty and ill-understood servlet, in an attempt to figure out what exactly it does. I know that it sets a bunch of cookies, but I don't know when, why, or what. It would be nice to just log all the cookies in the HttpServletResponse object at the end of the servlet's execution.
I know that cookies are typically the browser's responsibility, and I remember that there was no way to do this in .NET. Just hoping that Java may be different...
But if this isn't possible -- any other ideas for how to accomplish what I'm trying to do?
Thanks, as always.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只需要记录日志,那么我建议编写一个
javax.servlet.Filter
的实现,它将提供的HttpServletResponse
包装在一个包装器中,该包装器允许您公开 cookie过滤器执行后。像这样的事情:一个重要的警告:这不会向您显示哪些 cookie 被发送回浏览器,它只会向您显示应用程序代码添加到响应中的 cookie。如果容器选择更改、添加或忽略这些 cookie(例如,会话 cookie 由容器而不是应用程序处理),您将不知道使用此方法。但这对您的情况可能并不重要。
唯一确定的方法是使用浏览器插件,例如 Live Http Headers for Firefox 或 man-in -中间 HTTP 日志记录代理。
If logging is all you're after, then I suggest writing an implemention of
javax.servlet.Filter
which wraps the suppliedHttpServletResponse
in a wrapper which allows you to expose the cookies after the filter executes. Something like this:One big caveat: This will not show you what cookies are sent back to the browser, it will only show you which cookies the application code added to the response. If the container chooses to change, add to, or ignore those cookies (e.g. session cookies are handled by the container, not the application), you won't know using this approach. But that may not matter for your situation.
The only way to be sure is to use a browser plugin like Live Http Headers for Firefox, or a man-in-the-middle HTTP logging proxy.
我遇到了同样的问题,我使用接受 HttpServletResponse 的第三方库,并且我需要读回它在我的响应对象上设置的 cookie。为了解决这个问题,我创建了一个 HttpServletResponseWrapper 扩展,它在我拨打电话后为我公开这些 cookie:
以及我使用它的方式:
I had the same problem where I was using a 3rd party library which accepts an HttpServletResponse and I needed to read back the cookies that it set on my response object. To solve that I created an HttpServletResponseWrapper extension which exposes these cookies for me after I make the call:
And the way I use it:
您唯一的方法是包装 HttpServletResponse 对象,以便 addCookie 方法可以在设置 cookie 时拦截并记录日志。您可以通过添加 ServletFilter 来完成此操作,该过滤器在将现有 HttpServletResponse 传递到 Servlet 之前对其进行包装。
Your only approach is to wrap the HttpServletResponse object so that the addCookie methods can intercept and log when cookies are set. You can do this by adding a ServletFilter which wraps the existing HttpServletResponse before it is passed into your Servlet.
Cookie 通过“Set-Cookie”响应标头发送到客户端。试试这个:
Cookies are sent to the client in the "Set-Cookie" response header. Try this: