在头刷新后,在Java中的响应中添加一个cookie? [英] Adding a cookie to the response in Java after the header has been flushed?

查看:490
本文介绍了在头刷新后,在Java中的响应中添加一个cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义标记,进行一些处理,然后设置一个cookie。然而,cookie没有设置,我不知道为什么。另一个开发人员指出,因为我们使用模板系统,标签评估的点,响应头已经刷新作为include的一部分。因为头已经发送,它看起来不可能添加cookie(但是,当我尝试这样做时,没有状态异常抛出)。有办法解决这个问题吗?这听起来像是这个问题吗?

解决方案

像其他人说的,你真的想避免这样做首先。你可能想要一个不错的MVC类型的模型,你有一些servlet,做你的繁重的工作,如数据库和cookie和所有这些,然后传递控制到实际呈现响应的JSP。因为在你的servlet完成之后,JSP不会被调用来生成HTML,所以你不应该像这样遇到悲伤。



setCookie doesn当它失败时告诉你,但它也是有道理的,可能你不想要它打破整个页面。 ServletResponse.isCommitted()会告诉你头文件是否已经写入,因此你的setCookie()调用会失败。



如果你处于困境,为了能够做到这一点(当你寻找一个更好的解决方案),你可以创建一个servlet过滤器缓冲内存中的响应,直到你的cookie设置。模式将是这样:

  doFilter(request,response,chain)
{
BufferedResponse bufferedResponse = new BufferedResponse(response);
try
{
//将控制传递给下一个过滤器或传递给处理请求的JSP / servlet
chain.doFilter(request,bufferedResponse);
}
finally
{
bufferedResponse.flush(); BufferedResponse需要实现HttpServletResponse,基本上保持所有的内容在一个实例中。内存,直到你显式刷新它。在这一点上,它会写出头部,cookie和缓冲的响应主体。



这将完全工作,但它会导致您的Web服务器使用一堆额外的内存,因为它必须为每个请求缓冲内存中的整个响应体。此外,您的网页将加载速度较慢,因为服务器无法开始向客户端浏览器发送任何内容,直到您的网页完成后。 Bad juju我的朋友。


I have a custom tag that does some processing and then sets a cookie. However, the cookie wasn't being set, and I couldn't figure out why. Another developer pointed out that because we're using a template system, the point at which the tag evaluates, the response header has already been flushed as a part of an include. Since the header has been sent, it doesn't look possible to add the cookie (however, no state exceptions are thrown when I try to do it). Is there a way around this? Does this sound like that might be the problem?

解决方案

Like other folks have said, you really want to avoid having to do this in the first place. You'd like maybe a nice MVC kind of model where you have some servlets that do your heavy work like database and cookies and all that, and then pass control off to a JSP that actually renders the response. Since the JSP isn't called to generate HTML until after your servlets are done, you shouldn't run into grief like this.

It is kind of lame that setCookie doesn't tell you when it fails, but it also makes sense that probably you don't want it to break your entire page. ServletResponse.isCommitted() will tell you if the headers are already written, and consequently if your setCookie() call will fail.

If you are in dire straits and absolutely need to be able to do this (while you look for a better solution), you could create a servlet filter to buffer the response in memory until after your cookie is set. The pattern would be something like this:

doFilter(request, response, chain)
{
  BufferedResponse bufferedResponse = new BufferedResponse(response);
  try
  {
     // pass control to the next filter or to the JSP/servlet servicing the request
     chain.doFilter(request, bufferedResponse);
  }
  finally
  {
     bufferedResponse.flush();
  }
}

BufferedResponse would need to implement HttpServletResponse and basically keep everything in memory until you explicitly flush it. At that point it would write out the headers, cookies, and the buffered response body.

This will totally work, but it will cause your web server to use a bunch of extra memory since it has to buffer the entire response body in memory for every request. Also, your pages will load slower since the server can't start sending anything to the client browser until after your page is completely done. Bad juju my friend.

这篇关于在头刷新后,在Java中的响应中添加一个cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆