Filters in Servlets and JSP are Java classes, and their purpose of existence is as follows:
Intercept when requesting access to a backend resource
Manage responses returned from the server to the client
Several common filter types are listed below:
Authentication filter
Data compression filter
encryption filter
Filters that trigger resource access events
Image conversion filter
Login and authentication filters
MIME type chain filter
Token filter
XSL/T filters for transforming XML content
The filter will be inserted into the web.xml file and mapped to the name of the servlet, JSP file, or URL pattern. The deployment description file web.xml can be found in the <Tomcat-installation-directory>conf directory.
When the JSP container starts a web application, it creates an instance of each filter. These filters must be declared in the deployment descriptor file web.xml and executed in the order in which they are declared.
A filter is a Java class that implements the javax.servlet.Filter interface. The javax.servlet.Filter interface defines three methods:
| serial number | Method & Description |
|---|---|
| 1 | public void doFilter (ServletRequest, ServletResponse, FilterChain) The container will call this method whenever a request/response passes through the filter chain, because the client requests resources at the end of the chain. |
| 2 | public void init(FilterConfig filterConfig) The container calls this method to indicate that a filter is placed in the service |
| 3 | public void destroy() This method is called by the container to indicate that a filter is being removed from the service |
This example will print the IP address and the date and time of each access to the JSP file. Of course, this is just a simple example to give you some idea of simple filter usage, but you can use these concepts to construct more complex programs on your own.
//Introduce the Java package import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*; //Implement the Filter class public class LogFilter implements Filter { public void init( FilterConfig config) throws ServletException{ // Get initialization parameters String testParam = config.getInitParameter("test-param"); //Print initialization parameters System.out.println("Test Param: " + testParam); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // Get the client ip address String ipAddress = request.getRemoteAddr(); // Output ip address and current time System.out.println("IP "+ ipAddress + ", Time " + new Date().toString()); // Pass the request filter chain chain.doFilter(request,response); } public void destroy( ){ /* After the Filter instance is destroyed on the server Called before removal. */ }}Compile the LogFilter.java file, and then place the compiled class file in the <Tomcat installation directory>/webapps/ROOT/WEB-INF/classes directory.
Filters are defined and then mapped to a URL or JSP file name, much like how servlets are defined and mapped. In the deployment description file web.xml, use the <filter> tag for filter mapping:
<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value >Initialization Paramter</param-value> </init-param></filter><filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
The above filter will be applied to all servlets and JSP programs because we specified "/*" in the configuration. You can also specify a servlet or JSP path if you only want to apply the filter to a few servlets or JSP programs.
Now, access the servlet or JSP page as usual, and you will find a record of this access in the server log. You can also use the Log4J logger to log to other files.
Your web application can define many different filters. Now that you have two filters defined, AuthenFilter and LogFilter, the other steps are the same as before, except you create a different mapping, like this:
<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value >Initialization Paramter</param-value> </init-param></filter> <filter> <filter-name>AuthenFilter</filter-name> <filter-class>AuthenFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping> <filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
The mapping order of <filter> elements in web.xml determines the order in which the container applies these filters. To reverse the order of application, you simply reverse the order in which the <filter> elements are defined in web.xml.
For example, the above example would apply the LogFilter first and then the AuthenFilter, but the following example would reverse the order of application:
<filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping> <filter-mapping> <filter-name>LogFilter</filter -name> <url-pattern>/*</url-pattern></filter-mapping>