Differences between ServletResponse and HttpServletResponseWrapper?

I am new to servlet and reading text about filters and wrappers. I can understand the filters, but got confused in the wrappers. In the book, the author gives an example:

In the absence of a wrapper:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String name = request.getParameter("name").trim(); try { chain.doFilter(request, response); PrintWriter out = response.getWriter(); if (name.length() == 0) { out.println("Some message"); out.println("</body>"); out.println("</html>"); out.close(); } } catch (Throwable t) { } } 

In case of shell:

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String name = request.getParameter("name").trim(); HttpServletResponse httpRes = (HttpServletResponse) response; HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(httpRes); try { chain.doFilter(request, response); PrintWriter out = resWrapper.getWriter(); // why dont we just use response.getWriter(); if (name.length() == 0) { out.println("<h3>Some message"); out.println("</body>"); out.println("</html>"); out.close(); } } catch (Throwable t) { } } 

Why do we need HttpServletResponseWrapper , and we can do the same with ServletResponse in case 1? Can someone give me a clear example that we MUST use HttpServletResponseWrapper instead of ServletResponse ? I tried Google but did not find any luck.

+10
java servlets servlet-filters wrapper
Aug 11 2018-11-11T00:
source share
2 answers

BalusC's answer is good, but can be a bit overwhelming if you are just starting out.

Simply put: SerlvetResponse and its HttpServletResponse extension are interfaces that tell you what methods to call to do what you need. In the ordinary course of working with filters, servlets, etc., you often use HttpServletResponse to tell your application how to respond to requests.

HttpServletResponseWrapper is one specific implementation of HttpServletResponse, which gives you a convenient way to wrap an existing response with some logic without having to write a completely new interface implementation. He has many methods, so itโ€™s really nice. As a trivial example, suppose you wanted to block calls to response.flushBuffer () . This code using HttpServletResponseWrapper will do the following:

 class DisallowFlushResponseWrapper extends HttpServletResponseWrapper { public DisallowFlushResponseWrapper(HttpServletResponse response) { super(response); } @Override public void flushBuffer() { throw new UnsupportedOperationException("Don't call this!"); } } 

A typical way to use such a shell would be to create such a filter:

 class DisallowFlushFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { if (response instanceof HttpServletResponse) { HttpServletResponse newResponse = new DisallowFlushResponseWrapper((HttpServletResponse) response); chain.doFilter(request, newResponse); } ... } ... } 

Note that we complete the response included in the filter with an instance of our own shell. Then we pass the wrapper to the next element in the filter chain. Thus, everything that comes after this filter will get an exception if it calls flushBuffer (), because it will call it on our wrapper. The wrapper, due to the default behavior, will delegate any other call to the wrapped answer that is real, so everything except calls to this method will work fine.

+18
Aug 12 '11 at 23:15
source

This is a really stupid example that does not show the benefits of a request / response shell. In fact, the entire filter example is bad. HTML highlighting should be done using JSP or the highest servlet (but also for everyone). Go to our wiki filter page to get an idea of โ€‹โ€‹which filter you can use for.

The response shell is useful if you want to change the behavior of the response or just want to collect information about the response , while it is used in the request-response chain. Modified behavior occurs when a servlet or JSP invokes a specific response method. If you redefined it in your wrapper class, then this one will be called instead. You can change the behavior or collect information there.

Here at Stackoverflow, you can find some specific examples of useful implementations of HttpServletResponseWrapper .

+8
Aug 12 '11 at 10:16
source



All Articles