13 Filters
This chapter includes the following sections:
Overview of Filters
A filter is a Java class that is invoked in response to a request for a resource in a Web application. Resources include Jakarta servlets, Jakarta Server pages (JSP), and static resources such as HTML pages or images. A filter intercepts the request and can examine and modify the response and request objects or execute other tasks.
A filter applies cross-cutting request or response behavior to one or more resources without duplicating that behavior in each servlet or page. Common uses include authentication integration, request logging, request normalization, security headers, and response transformation. Keep filter chains small and measure filters that buffer or transform response bodies, because they can increase memory use and latency.
How Filters Work
You define filters in the context of a Web application. A filter intercepts a request for a specific named resource or a group of resources (based on a URL pattern) and executes the code in the filter. For each resource or group of resources, you can specify a single filter or multiple filters that are invoked in a specific order, called a chain.
When a filter intercepts a request, it has access to the jakarta.servlet.ServletRequest and jakarta.servlet.ServletResponse objects that provide access to the HTTP request and response, and a jakarta.servlet.FilterChain object. The FilterChain object contains a list of filters that can be invoked sequentially. When a filter has completed its work, the filter can either call the next filter in the chain, block the request, throw an exception, or invoke the originally requested resource.
After the original resource is invoked, control is passed back to the filter at the bottom of the list in the chain. This filter can then examine and modify the response headers and data, block the request, throw an exception, or invoke the next filter up from the bottom of the chain. This process continues in reverse order up through the chain of filters.
Note:
The filter can modify the headers only if the response has not already been committed.
Uses for Filters
Filters can be useful for the following functions:
-
Implementing a logging function
-
Implementing user-written security functionality
-
Debugging
-
Encryption
-
Data compression
-
Modifying the response sent to the client. (However, post processing the response can degrade the performance of your application.)
Writing a Filter Class
To write a filter class, implement the jakarta.servlet.Filter interface.
See https://jakarta.ee/specifications/platform/11/apidocs/jakarta/servlet/filter. You must implement the following methods of this interface:
-
init() -
destroy() -
doFilter()
You use the doFilter() method to examine and modify the request and response objects, perform other tasks such as logging, invoke the next filter in the chain, or block further processing.
Several other methods are available on the FilterConfig object for accessing the name of the filter, the ServletContext and the filter's initialization attributes. For more information see the Jakarta EE javadocs for jakarta.servlet.FilterConfig at https://jakarta.ee/specifications/platform/11/apidocs/jakarta/servlet/filterconfig.
To access the next item in the chain (either another filter or the original resource, if that is the next item in the chain), call the FilterChain.doFilter() method.
Configuring Filters
Configure a filter with @WebFilter when annotation-based configuration is appropriate. Use web.xml when the application requires descriptor-based mapping, dispatcher configuration, or explicit chain ordering.
import jakarta.servlet.Filter;
import jakarta.servlet.annotation.WebFilter;
@WebFilter(urlPatterns = {"/protected/*"})
public class RequestFilter implements Filter {
...
}The following sections describe descriptor-based filter declarations and mappings.
Configuring a Chain of Filters
WebLogic Server creates a chain of filters by creating a list of all the filter mappings that match an incoming HTTP request. The ordering of the list is determined by the following sequence:
-
Filters where the
filter-mappingelement contains aurl-patternthat matches the request are added to the chain in the order they appear in theweb.xmldeployment descriptor. -
Filters where the
filter-mappingelement contains aservlet-namethat matches the request are added to the chain after the filters that match a URL pattern. -
The last item in the chain is always the originally requested resource.
In your filter class, use the FilterChain.doFilter() method to invoke the next item in the chain.
Filtering the Servlet Response Object
You can use filters to post-process the output of a servlet by appending data to the output generated by the servlet. However, in order to capture the output of the servlet, you must create a wrapper for the response. (You cannot use the original response object, because the output buffer of the servlet is automatically flushed and sent to the client when the servlet completes executing and before control is returned to the last filter in the chain.) When you create such a wrapper, WebLogic Server must manipulate an additional copy of the output in memory, which can degrade performance.
For more information on wrapping the response or request objects, see jakarta.servlet.http.HttpServletResponseWrapper and jakarta.servlet.http.HttpServletRequestWrapper at https://jakarta.ee/specifications/platform/11/apidocs/jakarta/servlet/http/package-summary.html.
Additional Resources
-
Servlet 6.1 specification at
https://jakarta.ee/specifications/servlet/6.1/ -
Jakarta EE API Reference (Javadocs) at
https://jakarta.ee/specifications/platform/11/apidocs/