Filter and its function introduction

1. Filter Filter

    1. Introduction to filter

    Filter is the filtering of the client's access to resources. It is released when the conditions are met, but not when the conditions are not met, and logical processing can be performed before and after the target resource is accessed.

    2. Detailed API of Filter

        (1) The filter life cycle and its methods related to the life cycle

        The Filter interface has three methods, and these three are methods related to the life of Filter

            init(Filterconfig) : Represents the filter object initialization method, which is executed when the filter object is created

            doFilter(ServletRequest,ServletResponse,FilterChain) : The core method for performing filtering on behalf of the filter. If a resource has been configured to be filtered by this filter, the doFilter method will be executed every time the resource is accessed.

            destroy() : Represents the filter destruction method. This method is executed when the filter object is destroyed.

       The life cycle of the Filter object:

            When filter is created: the filter object is created when the server starts

            When is the filter destroyed: the filter is destroyed when the server shuts down

    (2) Detailed AP of Filter

          1)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.

                @Override
            //Initialization method: execute the init method when the Filter is created
            public void init(FilterConfig filterConfig) throws ServletException {
            //1. Get the name of the filter in web.xml <filter-name>QuickFilter1</filter-name>
            System. out.println(filterConfig.getFilterName());
            //2. You can also get some initialization parameters of the current filter
            System.out.println(filterConfig.getInitParameter("aaa"));
            //3. Get servletContext
            filterConfig.getServletContext( );
            System.out.println("init....");

           }

    2) destroy() method

        Executed when the filter object is destroyed

   3) doFilter method

        doFilter(ServletRequest,ServletResponse,FilterChain)

        Among the parameters:

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

        FilterChain : The filter chain object, the request can be released through the doFilter method of the object

                 

    4. Filter configuration

          <filter>
              <filter-nameFilter1</filter-name>
              <filter-class>com.filter.Filter1</filter-class>
      </filter>
      <filter-mapping>
              <filter-name>Filter1</filter-name>
              <url-pattern>/*</url-pattern>
      </filter-mapping>

    5. When url-pattern is configured

        1) Exactly match  /sertvle1

        2) The directory matches  /aaa/bbb/* ---- the most

            /user/*: access to foreground resources to enter this filter

            /admin/*: Execute this filter when accessing resources in the background

        3) The extension matches  *.abc *.jsp

    Note: url-pattern can be replaced by servlet-name or mixed

    6, dispatcher: access method (understand)

        The configuration format is: <dispather>*</dispather>

        REQUEST : The default value, which means that the filter is executed when a resource is directly accessed

        FORWARD : filter is only executed when forwarding

        INCLUDE: Execute filter when resources are included

        ERROR : jumping when an error occurs is to execute filter

    7. What is the role of Filter?

        1) Extraction of public code

        2) The methods in request and response can be enhanced (decorator pattern/dynamic proxy)

        3) Perform permission control


Guess you like

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