0 x00 preface

Filters, as they are called, are a new feature in Servlet 2.3 and are the most useful technology in Serlvet technology.

Filters actually intercept Web resources, do some processing and then hand over to the next filter or Servlet for processing, usually used to intercept requests for processing, can also intercept the response returned.

By using filter technology, developers can manage all Web resources, such as access control, filtering sensitive terms, compression of response information and other advanced functions.

0x01 Filter configuration

The configuration of a filter is similar to that of a Servlet, consisting of two groups of labels: filter and filter-mapping. Similar to a Servlet, a filter whose version is later than 3.0 can be configured using annotations

1. Web.xml based configuration

Here is a configuration based on web.xml


      
<web-app xmlns="http://java.sun.com/xml/ns/javaee"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
         version="2.5">
  
  <display-name>manage</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <description></description>
    <display-name>user</display-name>
    <servlet-name>user</servlet-name>
    <servlet-class>com.sec.servlet.UserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>user</servlet-name>
    <url-pattern>/user</url-pattern>
  </servlet-mapping>
  
  <filter>
    <display-name>test</display-name>
    <filter-name>test</filter-name>
    <filter-class>com.sec.test.test</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>test</filter-name>
    <url-pattern>/test</url-pattern>
  </filter-mapping>
</web-app>
Copy the code

The meanings of the labels are as follows:

<filter> Specify a filter <filter-name> Specify a name for the filter, <filter-class> The full qualified class name used to specify the filter <init-param> Used to specify the initialization parameter <param-name> of <init-param> for the filter, Used to specify that the name of the parameter <param-value> is a subparameter of <init-param>, <filter-mapping> Specifies the parameter value. <filer-name> specifies the registered name of a filter. <filer-mapping> Specifies the sub-parameter of the resource that a filter intercepts. The value must be the name of the filter declared in the <filter> element. <url-pattern> Is used to set the request path that the filter intercepts. <servlet-name> Is used to specify the name of the servlet that the filter interceptsCopy the code

Annotation-based approach

Sample code is as follows

import java.io.IOException;

@WebFilter(description="this is filter test",urlPatterns={"/filterTest"})
public class filterTest implements Filter {
    public void destroy(a) {
        /* Call */ when destroyed
    }
    
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /* The filter method basically does some processing of the request and response and then passes it to the next filter or Servlet  
        chain.doFilter(req, resp);// Pass it to the next filter or servlet
    }
    
    public void init(FilterConfig config) throws ServletException {
        /* The initialization method receives a parameter of type FilterConfig which is some configuration of Filter */}}Copy the code

As with servlets, any filter attribute that can be configured using web.xml can be configured using annotations, but annotation configuration is generally not recommended because you can control the order in which filters are executed using Web.xml rather than annotations.

0x02 Filter flow

The process of filter is very simple, as follows:

The user sends a request to the server server | | | filter 1... | | filter n service () method | filter n |... | filter 1 | | server returns the results to the userCopy the code

First the user makes a request to the server, then the request passes through the filter to the Service () method in the Servlet, and finally passes through the filter back to the user.

It is worth noting here that the filter receives and returns requests in reverse order.

0x03 Filter interface method

The filter interface method is as follows:

1. Init () interface

This interface is similar to the init() method used in servlets to initialize filters. If the FilterConfig object is used in the initialization code, it can only be written in the init() method of filter.

The init() method is defined as follows:

public void init(FilterConfig config) throws ServletException {
        /* The initialization method receives a parameter of type FilterConfig which is some configuration of Filter */
}
Copy the code

2. DoFilter () interface

The doFilter method is similar to the service() method of the Servlet interface. Filter implements specific filtering operations through the interface. When a customer requests access to the URL associated with the filter, the Servlet filter first executes the doFilter method. The FilterChain parameter is used to access subsequent filters.

The doFilter() method is defined as follows:

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /* The filter method basically does some processing of the request and response and then passes it to the next filter or Servlet  
        chain.doFilter(req, resp);// Pass it to the next filter or servlet
}
Copy the code

3, destroy() interface

The destroy() interface is similar to the destroy() interface in servlets and is used to release resources opened by filter, such as shutting down databases.

The destroy() method is defined as follows:

public void destroy(a) {
        /* Call */ when destroyed
}
Copy the code

0x04 Filter life cycle

The filter life cycle is similar to the servlet life cycle, as shown in the following figure.

When the Web container is started, the filters are instantiated in the order declared in web.xml.

Depending on the situation, the doFilter() method may be called multiple times and then destroy() when the program is closed or uninstalled.

Reference article:

Blog.csdn.net/Soinice/art…

www.cnblogs.com/tanghaorong…

Blog.csdn.net/yuzhiqiang_…

Original link:

www.teamssix.com/211116-1125…

For more information, please follow my personal wechat official account: TeamsSix