Filter filter interception mode

Keywords: JSP Java xml

There are five intercepting methods for Filter filter. Use annotation to configure the dispatcherTypes property

REQUEST: by default, resources directly requested by the browser will be blocked by the filter

FORWARD: FORWARD access resources will be blocked by filters

INCLUDE: contains access resources

ERROR: ERROR jump resource

ASYNC: asynchronous access to resources

If it is a web.xml configuration, it is to configure its < dispatcher > < dispatcher > tag

Here, we use notes to explain REQUEST and FORWARD, which are also commonly used. Here, we use a Filter class and a Servlet class to demonstrate. The Filter class controls the interception method, and the Servlet controls the access method, that is, REQUEST forwarding

package com.zhiying.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Filter executed");
        chain.doFilter(request,response);
    }
}

 

package com.zhiying.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/a1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet1...");
        req.getRequestDispatcher("/index.jsp").forward(req,resp); //Accessing index.jsp in the form of request forwarding
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

As you can see, direct access to index.jsp is through the filter

As can be seen from the figure below, if we access index.jsp through request forwarding, we will not go through the filter

Of course, we can also demonstrate that if FORWARD: FORWARD access resources will be blocked by filters. Modify the Filter class as follows

package com.zhiying.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = DispatcherType.FORWARD)
// @WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Filter executed");
        chain.doFilter(request,response);
    }
}

As you can see below, direct access is not filtered

As you can see below, request forwarding passes through the filter

Let's take a look at the combination of the two. It is configured on the annotation. It can be accessed either directly or through forwarding. You can modify the Filter class

package com.zhiying.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.FORWARD})
// @WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = DispatcherType.FORWARD)
// @WebFilter(urlPatterns = "/index.jsp", dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Filter executed");
        chain.doFilter(request,response);
    }
}

 

374 original articles published, 242 praised, 50000 visited+
Private letter follow

Posted by mykmallett on Mon, 20 Jan 2020 22:14:04 -0800