Name

Filter

Synopsis

A filter processes a request on either side of the target content. If a web application is configured to filter requests aimed at particular targets (either servlets or paths), the server will instead call the doFilter() method of the first Filter installed for that resource. A FilterChain object passed to the doFilter() method can be used by the filter to call the next filter in the sequence, or to invoke the target resource if all filters have been processed. When the doFilter() method finishes executing, control is ceded back to the web server or the preceding filter.

The Filter interface also provides an init() method which will be called and allowed finish execution before any doFilter() methods are called.

public interface Filter {
// Public Instance Methods
   public abstract void destroy();  
   public abstract void doFilter(ServletRequest request, 
        ServletResponse response, 
        FilterChain chain) throws IOExceptionServletException;  
   public abstract void init(
        FilterConfig filterConfig) throws ServletException;  
}

Get Java Enterprise in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.