[Principle of Java Web Filter]

The servlet filter itself does not generate request and response objects, it only provides filtering.

Servlet filters can inspect the Request object and modify the Request Header and Request content before the servlet is called.

Check the Response object after the servlet is called, and modify the Response Header and Response content.

The web components that the servlet filter is responsible for filtering can be servlets, JSPs, or HTML files.

 

 

All servlet filter classes must implement the javax.servlet.Filter interface. This interface contains 3 methods that the filter class must implement:

init(FilterConfig): This is the initialization method of the servlet filter, which will be called after the servlet container creates an instance of the servlet filter.

In this method, you can read the initialization parameters of the Servlet filter in the web.xml file.

Note: Once a filter fails to start, it will cause the entire web application to fail to start.

doFilter(ServletRequest, ServletResponse, FilterChain): This method completes the actual filtering operation.

When a client requests access to a URL associated with a filter, the servlet container will first call the filter's doFilter method.

The FilterChain parameter is used to access subsequent filters. In this method, the chain.doFilter() method is called to call the doFilter() method of the subsequent filter in the filter chain. If there is no subsequent filter, then the client request is passed to the corresponding Web component.

If the chain.doFilter() method is not called in this method, the client request will not reach the accessed web component.

destroy(): The servlet container calls this method before destroying the filter instance. In this method, the resources occupied by the servlet filter can be released.



 

 

After implementing a filter, you need to register and set the resources it can intercept in web.xml. This can be done with the <filter> and <filter-mapping> elements.

Its configuration method is very similar to servlet, the following is the specific configuration code

<filter>

  <filter-name>testFilterConfig</filter-name>

  <filter-class>cn.itcast.filter.TestFilterConfigFilter</filter-class>

  <!-- Configure the initialization parameters of the current Filter-->

  <init-param>

 <param-name>name</param-name>

 <param-value>Tom</param-value>

  </init-param>

  <init-param>

 <param-name>password</param-name>

 <param-value>123456</param-value>

  </init-param>

</filter>

 

<filter-mapping>

  <filter-name>testFilterConfig</filter-name>

  <url-pattern>/*</url-pattern>

</filter-mapping>

 

/*Indicates that all urls need to be filtered by this filter, and multiple mappings can be set for the same Filter in the same web.xml file. If the same Filter program appears multiple times in a Filter chain, the interception process of this Filter program will be executed multiple times

 

1. The interceptor is based on the reflection mechanism of java, while the filter is based on the function callback

2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container

3. Interceptors can only work on action requests, while filters can work on almost all requests

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326290717&siteId=291194637