简体   繁体   English

使用ResposeWrapper的响应筛选器

[英]Response Filters using ResposeWrapper

I came across a portion of code that zips the servlet output before returning it to the user, it uses a custom ServletResponseWrapper -it's so famous if u know what I am talking about-, my questions are : 我遇到了一部分代码,在将servlet输出返回给用户之前先对其进行了ServletResponseWrapper ,它使用了自定义的ServletResponseWrapper如果您知道我在说什么,这就是很有名的-,我的问题是:

1 - now the function of intercepting the response is totally the responsibility of the response wrapper -through overriding the output stream it returns- and the filter has no effect in such behaviour ? 1-现在拦截响应的功能完全是响应包装器的责任-通过覆盖返回的输出流-过滤器对这种行为没有影响吗? Am I correct ? 我对么 ?

2- what happens if any of the servlets that the filters intercept its requests closed the output stream, will the code after chain.doFilter() be able to use this stream again ? 2-如果过滤器拦截其请求的任何servlet封闭了输出流,chain.doFilter()之后的代码将能够再次使用该流吗? and will the filter work ? 过滤器会工作吗?

3 why does the wrapped response solve the problem of the "output returns directly to the container before being intercepted by the filter" ... I mean why the control over the response is then returned to the filter ? 3为什么包装的响应解决了“输出在被过滤器拦截之前直接返回到容器”的问题……我的意思是为什么对响应的控制然后又返回到过滤器?

I've recently used the example code from this book for my project: Professional Java for Web Applications . 我最近在我的项目中使用了本书中的示例代码: Web应用程序的Professional Java There is a good example for a compression filter in chapter 9. 第9章提供了一个很好的压缩过滤器示例。

I'm not involved with the company behind the book. 我没有参与这本书背后的公司。

These are the answers to your question: 这些是您的问题的答案:

  1. Yes, you're correct. 是的,你是对的。 The filter wraps the original HttpServletResponse with a wrapper, and from this moment on the wrapper is responsible for managing the output stream (but not for closing the stream). 过滤器使用包装器包装原始HttpServletResponse ,从此刻开始,包装器将负责管理输出流(但不负责关闭流)。
  2. It's not a good idea at all to close the output stream in your own code, whether you use a wrapped HttpServletResponse or not. 无论您是否使用包装的HttpServletResponse ,关闭自己代码中的输出流都不是一个好主意。
  3. The control over the output stream isn't returned to the filter. 对输出流的控制不会返回到过滤器。 The execution of your web app is continued in the line after chain.doFilter() , and you can write some data to the wrapped response, if it's necessary. 继续在chain.doFilter()之后的一行中继续执行Web应用程序,并且如有必要,您可以将一些数据写入到包装的响应中。 But don't close the stream, neither in your filter, nor in your servlet(s). 但是,无论是在过滤器中还是在Servlet中,都不要关闭流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM