We can implement our OWN response

The Container already implements the HttpServletResponse interface; that’s what you get in the doFilter() and service() methods. But to get this compression filter working, we have to make our own custom implementation of the HttpServletResponse interface and pass that to the servlet via the chain.doFilter() call. And that custom implementation has to also include a custom output stream as well, since that’s the goal—to capture the output after the servlet writes to it but before it goes back to the client.

image with no caption

Q:

Q: Filters pass ServletRequest and ServletResponse objects to the next thing in the chain, NOT HttpServlet Response! So why are you talking about implementing HttpServletResponse?

A:

A: Filters were designed to be generic, and so officially, you’re right. If we thought one of our filters might be used in a non-web app, we’d be implementing the non-HTTP interface (ServletResponse), but today, the chances of someone developing non-HTTP servlets is close to zero, so we’re not worried. And since ServletResponse is the supertype of HttpServletResponse, there’s no problem passing an HttpServletResponse where a ServletResponse is expected.

image with no caption
image with no caption

She doesn’t know about the servlet Wrapper classes

Creating your own custom HttpServletResponse implementation would be a pain. Especially when all you want to implement are just a few of the methods. And since HttpServletResponse is an interface that extends another interface, to implement your own custom response, you’d have to implement everything in both HttpServletResponse and its superinterface, ServletResponse.

But fortunately, someone at Sun did that for you, by creating a support convenience class that implements the HttpServletResponse interface. All of the methods in that class delegate the calls to the underlying real response created by the Container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset