Filters are web components that intercept the web requests of the Servlet according to a path and manipulate the request or response.
The filter must implement the javax.servlet.Filter interface and implement at least one of the following three methods:
- init: This starts when the web application is deployed, and it is usually used to configure initial properties
- doFilter: This is the main method that manipulates the request before the servlet is called by the client
- destroy: This starts after the undeploy of the web application to close all unused instances or reset internal properties.
This sample manipulates the web response, adding some rows to the resulting page:
@WebFilter(filterName = "MyBarFilter", urlPatterns = { "/filtered/*" })
public class MyBarFilter implements Filter {
...
private FilterConfig filterConfig;
private void doBeforeProcessing...