Sunday, February 10, 2013

Head First Servlets and JSP: Chapter 13 Wrapper & Filters

Filters are managed by the container like servlets


you define a filter by implementing the Filter interface.

we use the chain.doFilter() to move to the next filter.

then you add the filter information in the DD


filters will be in the order appears in the DD

you can also specify if you want the filter to work in case of REQUEST of FORWARD ...

AFTER SERVLET FILTER
lets say you want to run a filter after the servlet is executed, you can add this code after chain.doFilter().


FILTER and SERVLET and RESPONSE
we were talking about the after servlet filter, lets say you want to compress the response, you cannot simply do that on the response object you have in the filter, because the stream will be written by the servlet, so inorder to do that you should wrap the response in a different response object, send it to the servlet, then compress it.

here is the filter code.



and you define the the new response class like this


No comments:

Post a Comment