JAVA filter (the Filter)

Introduction of a .Filter

filter is a filter for clients accessing resources, in line with the conditions of release, it does not meet the conditions of release, and can be logically processed before and after the target resource access

effect:

  • Extraction code (can be set to request coding, coding so that all are unified through the servlet, no need to write each one)
  • Rights Management (taken within the filter currently logged on user roles, access to resources, look, if the match is released, do not match is not released)
     

Two .Filter Detailed

Filter life cycle and associated with the life cycle approach

Filter interface has three methods, and this method is associated with three of the Filter Life 

  • init (Filterconfig): represents the filter object initialization method, executed when the filter object is created
  • doFilter (ServletRequest, ServletResponse, FilterCha): core methods filter performs filtering representatives, if a resource is filtered, then the filter has been configured to this, so each time you access this resource will be executed doFilter method
  • destory (): method of destruction filter is representative of the method is performed when the object is destroyed filter

Filter object life cycle: 

  • When Creating Filter: This filter object is created when the server starts
  • When the destruction Filter: filter destroyed when the server shuts down
  • Every time I visit the filtered filter resources, perform doFilter ()

The Filter API details

the init (the FilterConfig arg0) wherein the configuration information representing an object parameter arg0 Filter the object inside the package configuration information of the filter.

@Override
public void init(FilterConfig arg0) throws ServletException {
    //获得web.xml中<filter-name>QuickFilter1</filter-name>的名字
    System.out.println(arg0.getFilterName());
    //获得当前filter的初始化参数<init-param><param-name>aaa</param-name><param-value>bbb</param-value></init-param>
    System.out.println(arg0.getInitParameter("aaa"));
    //获得所有初始化参数的名字
    System.out.println(arg0.getInitParameterNames());
    //获得servletContext对象(故先有servletContext)
    System.out.println(arg0.getServletContext());
    System.out.println("init...");
}

Destory () method is executed when the filter object is destroyed

the doFilter (the ServletRequest the arg0, the ServletResponse arg1, arg2 the FilterChain) Filter the core filtration process, when the analytical parameters as follows: 

  • ServletRequest arg0: inside the package is a content client http requests
  • ServletResponse arg1: representatives of response
  • ServletRequest / ServletResponse: Every web doFilter in the implementation of the method container is responsible for creating a request and a response object passed in as a parameter of doFilter. The request is in response to the service method when accessing the target resource request and response.
  • FilterChain: filter chain objects until all filter and the corresponding sequence by the method of the object can doFilter the release request [Note order filter is filter-mapping performed according to the order]

Chain filter

web.xml define multiple filters in filter order of the filter and define the order in web.xml same filter. Each call object FilterChain the doFilter () method, servlet container checks whether there are objects FilterChain Filter, if followed by filtration; if no resource interface operation proceeds

Filter configuration

<filter>
  <filter-name>QuickFilter1</filter-name>
  <filter-class>filter.QuickFilter1</filter-class>
  <init-param>
      <param-name>aaa</param-name>
      <param-value>bbb</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>QuickFilter1</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher><!-- 直接访问某个资源时执行filter -->
</filter-mapping>
<filter>
  <filter-name>QuickFilter2</filter-name>
  <filter-class>filter.QuickFilter2</filter-class>
</filter>
<filter-mapping>
  <filter-name>QuickFilter2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>FORWARD</dispatcher><!-- 转发时才执行filter -->
</filter-mapping>

Item Description:

<Filter> are configuration items:

    <Filter-name>: name of the filter

    <Filter-class>: The filter configuration corresponding to the class

    <Inti-param>: initialization parameter

    <Param-name>: Parameter Name

    <Param-value>: Parameter Value 

<Filter-mapping> in configuration items:

    <Filter-name>: Filter Name, and <filter> The <filter-name> same

    <Url-pattern>: resource path filters

    <Dispatcher>: Specifies the filters is called by resources, the default is REQUEST

url-pattern configuration

  • Exact match / sertvle1
  • Directory matching / aaa / bbb / * (the most)
  • / User / *: resource access into the foreground of this filter
  • / Admin / *: Perform this filter when accessing resources background
  • Extension matches .abc .jsp

Note: url-pattern servlet-name may be used alternatively, it may be mixed, filtered represented specified servlet.

dispatcher: access to the

  • REQUEST: The default value, the filter behalf of direct access to a resource (this time it is forwarded visit once, twice redirect access)
  • FORWARD: forwards when executed filter
  • INCLUDE: (A case of performing resource comprising a resource B) comprising resources when executing the filter
  • ERROR: The jump is executed when an error occurs filter (executed when the jump error pages)
     

Guess you like

Origin blog.csdn.net/qq_42239765/article/details/90545916