在FilterChain.doFilter()-HttpServletResponseWrapper之后添加Cookie来忽略刷新? [英] Adding Cookie after FilterChain.doFilter() - HttpServletResponseWrapper to ignore flushing?

查看:434
本文介绍了在FilterChain.doFilter()-HttpServletResponseWrapper之后添加Cookie来忽略刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其内容(通常是HTML)呈现后,将Cookie添加到 HttpServletResponse 中。

I'd like to add a cookie to an HttpServletResponse after its content (usually HTML) has been rendered.

如此处所述( http://osdir.com /ml/java.jasig.uportal/2005-10/msg00276.html ),
和此处(),这可以通过以下方式实现:缓冲响应,使其不会被刷新并发送给客户端(因为将标头发送给客户端之后,响应才被提交,并且无法再将其他标头(即cookie标头)发送给客户端)。

As mentioned here (http://osdir.com/ml/java.jasig.uportal/2005-10/msg00276.html), and here (Adding a cookie to the response in Java after the header has been flushed?), this is possible by buffering up the response so it won't get flushed and sent to client (since after headers are sent to client, response is committed, and no more headers, namely cookie headers, can be sent to client).

假定这是手头的目标:我认为,实现此目标的一种可能方法是使用 HttpServletResponseWrapper ,我可以覆盖其 flushBuffer()方法并向客户端释放标头内容的实际刷新:

Assuming this is the goal at hand: I gather that a possible way to accomplish this is by use of an HttpServletResponseWrapper, I can override its flushBuffer() method and prevent the actual flushing of headers\content to client:

public class BufferedHttpServletResponse extends HttpServletResponseWrapper {

    public BufferedHttpServletResponse(HttpServletResponse response) {
        super(response);
    }

    public void flushBuffer() {
    }

}

并在 Filter 中应用此缓冲的响应,以供其余过滤器链使用:

and apply this buffered response in a Filter, for the rest of the filter chain to use:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    BufferedHttpServletResponse bufferedResponse = new BufferedHttpServletResponse(response);

    chain.doFilter(request, bufferedResponse);

    // ... create Cookie and add it to response ... response.addCookie(newCookie)
    // response.flushBuffer() ?        

}

问题:以上是否可以,或者是否已完全解决? ?在我的 doFilter()退出之后,响应的缓冲区是否会一直填充内容\标头(并可能会重新调整大小),然后将其刷新并发送给客户端通过其他过滤器还是通过servlet容器(Tomcat)?我应该自己刷新响应吗? ...在这里提到(应该在HttpServletResponse上调用.close()。 getOutputStream()/。getWriter()?)那个流不应该被关闭,但是在这种情况下应该刷新响应吗?

Question: is the above okay, or is it completely off? Will the response's buffer just keep getting filled with content\headers (and possibly getting re-sized) until after my doFilter() exits, to then be flushed and sent to client by other filters or by the servlet container (Tomcat)? Should I flush the response myself? ... mentioned here (Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()?) that stream should not be closed, but should response be flushed in this case?

不一定假设这是当前的目标:我的一般问题是,我需要为客户端浏览器设置Cookie,但是Cookie的值取决于呈现响应内容时发生的事情( Struts 1.0 Action,然后在Velocity模板中)-我知道仅在经历整个(Servlet-> HTML渲染)流程之后才想设置什么cookie值。

Not necessarily assuming this is the goal at hand: My general problem is that I need to set a cookie for a client browser, but the value of the cookie depends on stuff that happens while rendering the response's content (stuff that happens in a Struts 1.0 Action, and after that in a Velocity template) - I know what cookie value I want to set only after going through the entire (Servlet -> Rendering of HTML) flow.

请赐教。谢谢。

推荐答案

好像没人愿意接受这一点,我将尝试至少提供我的观点。

Looks like nobody is ready to take this one, I will give an attempt to provide at least my view point.

在回答您的问题之前,首先让我们看一下servlet过滤器的主要任务。

Before I get to your question First, Lets look at the main tasks of the servlet filter.


  • 查询请求并采取相应的行动。

  • 阻止请求与响应对进一步传递。

  • 修改请求标头和数据。您可以通过提供请求的
    定制版本来完成此操作。

  • 修改响应标头和数据。 您可以通过提供响应的
    定制版本来做到这一点。

  • Query the request and act accordingly.
  • Block the request-and-response pair from passing any further.
  • Modify the request headers and data. You do this by providing a customized version of the request.
  • Modify the response headers and data. You do this by providing a customized version of the response.

来源: http://docs.oracle.com/javaee/6/tutorial/doc/bnagb .html

现在回到您的问题,您可以通过自定义响应 ResponseWrapper

Coming back to your question now, You could modify the response headers by customizing the response a.k.a ResponseWrapper.

看看您的代码,您想要做的是修改原始响应,而不是修改自定义响应,并且(向原始响应添加cookie)不行

Looking at your code, what you're trying to do is modifying the original response instead of modifying the custom response and which (adding cookie to original response) is not OK.

因此,正确的方法是在您的自定义项中添加cookie,例如下面的示例。

So the proper way of doing it is to add the cookie in your custom something like the below sample.

   private class CustomResponseWrapper extends HttpServletResponseWrapper {

        private Collection<Cookie> myCookies = new ArrayList<Cookie>();

        public CustomResponseWrapper(HttpServletResponse response) {
            super(response);
        }

        @Override
        public void addCookie(Cookie cookie) {
            super.addCookie(cookie);
            myCookies.add(cookie);
        }
    }

这篇关于在FilterChain.doFilter()-HttpServletResponseWrapper之后添加Cookie来忽略刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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