Using this template you can design a simple Filter class.
A filter implements javax.servlet.Filter and defines its three methods:
- void setFilterConfig(FilterConfig config): Sets the filter's configuration object
- FilterConfig getFilterConfig(): Returns the filter's configuration object
- void doFilter(ServletRequest req, ServletResponse res, FilterChain chain): Performs the actual filtering work
The server calls setFilterConfig() once to prepare the filter for service, then calls doFilter() any number of times for various requests.
The FilterConfig interface has methods to retrieve the filter's name, its init parameters, and the active servlet context.
Each filter receives in its doFilter() method the current request and response objects, as well as a FilterChain containing the filters that still must be processed.
In the doFilter() method, a filter may do what it wants with the request and response objects(see the doBeforeProcessing method). The filter then calls chain.doFilter() to transfer control to the next filter. When that call returns, a filter can, at the end of its own doFilter() method, perform additional work on the request/response objects(see the doAfterProcessing method).
The most important methods that should be implemented in this template are :
- private void doBeforeProcessing(ServletRequest req, ServletResponse res): write code here to process the request and/or response before the rest of the filter chain is invoked.
- private void doAfterProcessing(ServletRequest req, ServletResponse res: write code here to process the request and/or response after the rest of the filter chain is invoked.