title: Differences between ServletResponse and HttpServletResponseWrapper?
body: <p>I am new to servlet and reading some text about filters and wrappers. I can understand filters but got confused about wrappers. In the book, the author gives an example:</p>
  
  <p>In case no wrapper:</p>
  
  <pre><code>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("&lt;/body&gt;");
                  out.println("&lt;/html&gt;");
                  out.close();
              }
          } catch (Throwable t) {
          }
      }
  </code></pre>
  
  <p>In case of wrapper:</p>
  
  <pre><code> 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("&lt;h3&gt;Some message");
                  out.println("&lt;/body&gt;");
                  out.println("&lt;/html&gt;");
                  out.close();
              }
          } catch (Throwable t) {
          }
      }
  </code></pre>
  
  <p>Why we need <code>HttpServletResponseWrapper</code>  while we can do the same thing with <code>ServletResponse</code> in case 1? Can anyone give me a clear example that we MUST use <code>HttpServletResponseWrapper</code>  instead of <code>ServletResponse</code>? I have tried to google but found no luck.</p>