2021-02-21-In-depth understanding of filters for Java projects

In-depth understanding of the filter filter of the Java project

concept

  • The filter is bound to the Web application in the form of a component. Unlike other web reference program components, the filter is processed in a "chain" manner.
  • Filter is a new function added after servlet2.3 in sun company
  • Define a filter and let a class implement the javax.servlet.Filter interface directly
  • Servlet: simple servlet, filtering servlet, listening servlet

Application scenarios

  • Login interception
  • Encoding filter
  • Permission Validation

Filter execution process

Insert picture description here

achieve

  • Define a class to implement the methods in the Filter interface replication
  • Configure in web.xml
 <filter>
	 <filter-name></filter-name>
	 <filter-class></filter-class>
  <filter>
  <filter-mapping>
	  <filter-name></filter-name>
	   <url-pattern> </url-pattern>
  </filter-mapping>
  • Filter should be configured on the Servlet
  • One can configure multiple

life cycle

method Description
public void init(FilterConfig filterConfig) Called during initialization (initialization when the container is started), the initialization parameters of the configuration can be obtained through filterConfig
public void doFilter(req,resp ,FilterChain chain) Complete the specific filtering operation, and then let the request continue to pass down through the FilterChain
public void destroy() Called when the filter is destroyed
  • life cycle
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113823388