Filters are new in the Sun Microsystems Java Servlet
2.3 Specification. They enable you to intercept to intercept a
request before it reaches a resource. In other words, a filter gives you
access to the HttpServletRequest and the HttpServletResponse objects
before they are passed on to a servlet.
Filters can be very useful. For example, you can write a filter
that records all incoming requests and logs the IP addresses of the
computers from which the requests originate. You also can use a filter
as an encryption and decryption device. Other uses include user
authentication, data compression, user input validation, and so on.
You also can put a set of filters in a chain. The first filter in
the chain will be called first and then pass control to the second
filter, and so on. Filter chaining ensures that you can write a filter
that does a specific task but adds some functionality in another filter.
When writing a filter, you basically deal with the
following three interfaces in the javax.servlet package:
-
javax.servlet.Filter: represents the
life-cycle of the filter.
-
javax.servlet.FilterConfig: represents the
configuration of the filter.
-
javax.servlet.FilterChain: object provided by
the servlet container to the developer giving a view into the
invocation chain of a filtered request for a resource.
For more information about filters, refer to the Sun Microsystems Java™ Servlet
2.3 Specification at java.sun.com/products/servlet/download.html.