The most important method in Filter interface is the doFilter method, which is the heart of the filter.
This method usually performs some of the following actions:
Examines the request headers
Customizes the request object if it wishes to modify request headers or data or block the request entirely
Invokes the next entity in the filter chain.
Customizes the response object if it wishes to modify response headers or data
If the current filter is the last filter in the chain that end with the target servlet,
the next entity is the resource at the end of the chain; otherwise, it is the next filter
that was configured in the WAR. It invokes the next entity by calling doFilter method on the chain object (passing in the request and response it was called with, or the wrapped versions it may have created). Alternatively, it can choose to block request by not making the call to invoke the next entity. In the latter case, the filter is responsible for the filling out the response.
Examines response headers after it has invoked the next filter in the chain
Throws an exception to indicate an error in processing
In addition to doFilter, you must implement init and destroy methods. The init method is called by the container when the filter is instantiated. If you wish to pass initialization parameters to the filter you retrieve them from the FilterConfig object passed to the init.
|