JAVAWEB-filter (Filter), the main writing steps, access methods and filter life cycle and API details

One. Introduction to Filter

Filter is to filter the client's access to resources. It can be released if the conditions are met, and not released if the conditions are not met, and it can perform logical processing before and after the target resource is accessed.

Two, preparation steps

  • Write a filter class that implements the Filter interface
  • Implement methods not yet implemented in the interface
  • In web.xmlconfiguring the (mainly configured to filter which resources)

Case: The
first step: implement the interface, rewrite the method

public class QuickFilter1 implements Filter{
    
    
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
    
    
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
    
    
		// TODO Auto-generated method stub
		System.out.println("quick1 running...");
		//放行请求
		chain.doFilter(request, response);
	}
	
	@Override
	public void destroy() {
    
    
		// TODO Auto-generated method stub
		
	}

Step 2: Configuration web.xmlfile

<filter>
  		<filter-name>QuickFilter1</filter-name>
  		<filter-class>com.filter.filter.QuickFilter1</filter-class>
  </filter>
  <filter-mapping>
  		<filter-name>QuickFilter1</filter-name>
  		<!--对该web应用下的所有的资源进行过滤  -->
  		<url-pattern>/*</url-pattern>
  </filter-mapping>

Step 3: Write a Servlet for testing

public class Servlet1 extends HttpServlet {
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
	response.getWriter().write("Servlet1 running...");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		doGet(request, response);
	}
}

note:
chain.doFilter(request, response)
If we do not write this code, we will not be able to access the content of Servlet1, if we write this code, then we will access the content of Servlet1 as we wish. This is a release request.

Three, Filter access process

When we configure <url-pattern>/*</url-pattern>, we filter our entire web application. In fact, when we access the Servlet,Will go through multiple filters, Multiple filters will form a chain, if you want to pass these. All need to carry out the above chain.doFilter(request,response)for release.
Insert picture description here

four. Filter life cycle and API detailed explanation

1.Filter life cycle and its related methods

The Filter interface has three methods:

  • init(Filterconfig): On behalf of the filter object initialization method executed when the filter object is created
  • doFilter(ServletRequest,ServletResponse,FilterChain): Represents the core method of filter execution. If a resource has been configured to this filter for filtering, then the doFilter method will be executed every time the resource is accessed
  • destory(): Represents the filter destruction method, which is executed when the filter object is destroyed

2. The life cycle of the Filter object:

  • When is the Filter created: the Filter object is created when the server starts
  • When is Filter destroyed: Filter is destroyed when the server is closed

3. Detailed API of Filter

  • init(FilterConfig)方法
    The parameter config represents the object of the configuration information of the Filter object, and the internal encapsulation is the configuration information of the filter.

Case:

The first step: write some web.xmlcontent first

<filter>
  		<filter-name>QuickFilter1</filter-name>
  		<filter-class>com.filter.filter.QuickFilter1</filter-class>
  		<init-param>
  			<param-name>aaa</param-name>
  			<param-value>AAA</param-value>
  		</init-param>
  </filter>
  <filter-mapping>
  		<filter-name>QuickFilter1</filter-name>
  		<!--对该web应用下的所有的资源进行过滤  -->
  		<url-pattern>/*</url-pattern>
  </filter-mapping>

Step 2: Use the init method to get the information in web.xml

@Override
	public void init(FilterConfig filterConfig) throws ServletException {
    
    
		//获得web.xml中的filter的名称——————
		//<filter-name>QuickFilter1</filter-name>
		filterConfig.getFilterName();
		//获得当前filter的初始化参数
		filterConfig.getInitParameter("aaa");
		//获得ServletContext
		filterConfig.getServletContext();
	}
  • destory()方法
    It is executed when the filter object is destroyed, and it can be executed when the server is closed.

  • doFilter方法
    doFilter(ServletRequest,ServletResponse,FilterChain)
    Among the parameters:

  • ServletRequest/ServletResponse: Every time the doFilter method is executed, the web container is responsible for creating a request and a response object as the parameters of the doFilter. The request and the response are the request and response when accessing the service method of the target resource.

  • FilterChain: Filter chain object, the request can be released through the doFilter method of the object. He will put our filter object into a FilterChain object. The order of the filter is executed according to the order in the written web.xml

Case:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Filter</display-name>
 
  <filter>
  		<filter-name>QuickFilter1</filter-name>
  		<filter-class>com.filter.filter.QuickFilter1</filter-class>
  		<init-param>
  			<param-name>aaa</param-name>
  			<param-value>AAA</param-value>
  		</init-param>
  </filter>
  <!-- 注意此处的filter-mapping-->
  <filter-mapping>
  		<filter-name>QuickFilter1</filter-name>
  		<!--对该web应用下的所有的资源进行过滤  -->
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
    <!-- 注意此处的filter-mapping-->
  <filter>
  		<filter-name>QuickFilter2</filter-name>
  		<filter-class>com.filter.filter.QuickFilter2</filter-class>
  </filter>
  <filter-mapping>
  		<filter-name>QuickFilter2</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

note:
In the web.xml, QuickFilter1 is <filter-mapping>in <filter-mapping>front of QuickFilter2 . Therefore, when using the filter, filter1 is filtered first and then filter2.
As shown:
Insert picture description here

4. Filter's web.xml configuration file

When we configure url-pattern, there are three ways of writing:

  • Exact match:/ sertvle1
  • Directory matching:/aaa/bbb/*
  • Extension matching:*.abc *.jsp

note: Url-pattern can be replaced by servlet-name, or mixed
as follows:

<filter-mapping>
  		<filter-name>QuickFilter1</filter-name>
  		<!--对该web应用下的所有的资源进行过滤  -->
  		<!-- <url-pattern>/*</url-pattern> -->
  		<servlet-name>Servlet1</servlet-name>
  </filter-mapping>

Filter has another configuration method:

dispatcher

  • REQUEST: The default value, which means that the filter is executed when a resource is directly accessed
  • FORWARD: Filter is executed only when forwarding
  • INCLUDE: Execute filter when resources are included
  • ERROR: Jump when an error occurs is to execute filter

as follows:

<filter-mapping>
  		<filter-name>QuickFilter1</filter-name>
  		<!--对该web应用下的所有的资源进行过滤  -->
  		<!-- <url-pattern>/*</url-pattern> -->
  		<!-- <servlet-name>/Servlet1</servlet-name> -->
  		<url-pattern>/*</url-pattern>
  		<dispatcher>可以再次进行上述配置</dispatcher>
  </filter-mapping>

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/109299695