The basic concept

  • The Filter is one of the three components of JavaWeb: Servlet, Filter, Listener
  • Filters are Web service components that add processing capabilities to the request and response processing of a Web application
  • A filter acts as a filter between the browser and Web resources. You can use the filter to filter and operate requests before accessing resources. You can also modify, judge, and block responses

use

  • A custom class implements the Filter interface and overwrites the doFilter method
  • Configure filters in the web.xml file (similar to configuring servlets)
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    //TODO handles logic
    Calling this method means passing the filter
    filterChain.doFilter(servletRequest, servletResponse);
}
Copy the code
<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>com.yamin.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
Copy the code

Commonly used class

The Filter interface

  • Describes a filter object that filters requests and responses to resources
methods function
void init(FilterConfig filterConfig) Implements the initialization of the filter
void doFilter(ServletRequest req,ServletResponse res) Functional logic to perform filtering operations
void destory() Implement the filter destruction operation

The FilterConfig interfaces

  • This parameter describes the configuration of filters
methods function
String getFilterName() Gets the name of the filter
String getInitParameter(String name) Gets information about parameters that specify initialization
Enumeration getInitParameterNames() Get all initialization parameter information
ServletContext getServletContext() Get the ServletContext object

Filter chain

  • If multiple filters meet the filtering conditions, the container is based onThe sequence of mappingsTo call the individual filters, and then return the reverse order through the filters
  • For example, if the order of the three filters is 1 -> 2 -> 3, the order of returning filters is 3 -> 2 -> 1
<filter-mapping>
    <filter-name>myFilter1</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>myFilter2</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>myFilter3</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
Copy the code
Clear -- -- -- -- -- - a -- -- -- -- -- - a -- -- -- -- -- - a -- -- -- -- -- - take tabled worry about affairs filter please please -- -- -- -- -- machine -- -- -- -- -- machine please please -- -- -- -- -- machine -- -- -- -- -- 1 2 3Copy the code

Advantages of the Filter

  • The “pluggable” implementation code, even if the increase or decrease of a functional module, will not affect the normal implementation of the system
  • Multiple modules with the same processing logic can be centrally written in the filter, which can be reused and convenient for code maintenance