Java Web: Filter

Posted by azaidi on Tue, 18 Jan 2022 15:33:29 +0100

1, What is a filter

1. Filter is one of the three major components of Java Web. The three components are: Servlet program, Listener listener and filter filter

2. Filter is a Java EE specification. That is, the interface.

3. The function of Filter is to intercept requests and Filter responses.

Common application scenarios:

1. Permission check

2. Journal operation

3. Transaction management

                . . . . . wait

 

2, Use steps of Filter

1. Write a class to implement the Filter interface

2. Implement the filtering method doFilter ()

        

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        HttpServletRequest req = (HttpServletRequest) servletRequest;
        req.getSession().setAttribute("user","hello!");
        Object user = req.getSession().getAttribute("user");

        System.out.println("I am doFilter interceptor method ");

        if(user == null){
            //Not logged in: blocked. Let users log in
            servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
            return;
        } else {
            //Let the program continue to access the customer's target resources
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }

3. On the web Configure the interception path of Filter in XML

<filter>
    <!--to filter Create an alias-->
       <filter-name>MyFilter</filter-name>
       <!--to configure filter Full class name of-->
       <filter-class>com.caiyingbin.web.MyFilter</filter-class>     
</filter>
<filter-mapping>
     <filter-name>MyFilter</filter-name>
     <url-pattern>/admin/*</url-pattern>
</filter-mapping>

        

3, Filter lifecycle

The life cycle of Filter contains several methods

1. Constructor method

2. init() initialization method

Steps 1 and 2 are executed when the web project starts (the Filter has been created)

3. doFilter() filtering method

Step 3: every time a request is intercepted, it will be executed

4. destroy

Step 4: when the web project is stopped, it will be executed (stop the web project and destroy the Filter)

  

4, FilterConfig class

FilterConfig is the configuration file class of Filter filter

Every time Tomcat creates a Filter, it will also create a FilterConfig class, which contains the configuration information of the Filter configuration file.

The function of FilterConfig class is to obtain the configuration content of Filter filter

1. Get the name of Filter < Filter name >

2. Get the init param initialization parameters configured in Filter

3. Get ServletContext object

@Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("I am init()Initialization method");

        System.out.println("FilterConfig Function 1: get<filter-name>Value of");
        String filterName = filterConfig.getFilterName();
        System.out.println("I am filter-name Value of:" + filterConfig);

        System.out.println("FilterConfig Function 2: get<init-param>Value of parameter");
        String username = filterConfig.getInitParameter("username");
        String url = filterConfig.getInitParameter("url");
        System.out.println("user name:" + username);
        System.out.println("Resource path" + url);

        System.out.println("FilterConfig Function 3: get servletContext object");
        ServletContext servletContext = filterConfig.getServletContext();
        System.out.println("I am ServletContext Object:" + servletContext);

    }

web. Configuration in XML

<filter>
       <!--to filter Create an alias-->
       <filter-name>MyFilter</filter-name>
      <!--to configure filter Full class name of-->
       <filter-class>com.caiyingbin.web.MyFilter</filter-class>
            
       <init-param>
             <param-name>username</param-name>
             <param-value>root</param-value>
       </init-param>
       <init-param>
            <param-name>url</param-name>
            <param-value>http://localhost:8080/15_filter/</param-value>
        </init-param>
</filter>
<filter-mapping>
      <filter-name>MyFilter</filter-name>
      <url-pattern>/admin/*</url-pattern>
</filter-mapping>

5, FilterChain filter chain

Filter

Chain chain

FilterChain , is the filter chain (multiple filters work together)

6, Intercepting path of Filter

1. Accurate matching

                <url-pattern>/target.jsp</url-pattern> 

The path configured above indicates that the request address must be: http://ip:port/ Project path / target jsp

2. Directory matching

                <url-pattern>/admin/*</url-pattern>

The path configured above indicates that the request address must be: http://ip:port/ Project path / admin/*

3. Suffix matching

               <url-pattern>*.html</url-pattern>

The path configured above indicates that the request address must be in The end of html will be intercepted

                <url-pattern>*.html</url-pattern>

The path configured above indicates that the request address must be in It will be intercepted at the end of do

                <url-pattern>*.html</url-pattern>

The path configured above indicates that the request address must be in It will be intercepted at the end of action

        

The Filter only cares about whether the requested address matches, not whether the requested resource exists.

Topics: JavaEE Tomcat intellij-idea