-1

I am new to Servlets. In the book i am reading now it is written, that we need wrappers, because it is late to do anything with response after finishing chain.doFilter() method as response is sent already.

I wrote the following Servlet and Filter:

public class MyServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.println("In Servlet");
    }
}

public class MyFilter implements Filter{
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        chain.doFilter(request, response);
        writer.println("After chain");
    }
}

And i see both strings in the browser. My question is: Why do we need wrappers? I still can write to response even after chain.doFilter and i still see result?

Is it because response is sent in two pieces(first in the end of chain.doFilter and second in the end of Filter.doFilter)? So if i had to compress response it would work incorrectly(because first uncompressed part would be sent)?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dilia
  • 1
  • 1
  • Please format your code so it becomes more readable. For more information, see [this](https://stackoverflow.com/editing-help) page. – Martin Aug 12 '15 at 09:04

1 Answers1