Interceptors and Filters

filter

A filter is a program that runs on the server before its associated servlet or JSP page. It is started when your web application starts, it is initialized only once, and related requests can be intercepted later, and it is destroyed only when your web application is stopped or redeployed.
It is
used to filter requests and responses. The incoming request and response filter out some information in advance, or set some parameters in advance, and then pass in the action of servlet or struts for business logic, such as filtering out illegal URLs (not the address of login.do Requests, if the user is not logged in, they will be filtered out), or uniformly set the character set before the action of the incoming servlet or struts, or remove some illegal characters (often used in chat rooms, some curse words).

The basic principle of the servlet filter is
to perform preprocessing after the request enters the container, but not before entering the servlet; post-processing is performed before the request is returned to the front end. Once processed, it goes to the next filter for processing until the request is sent to the target.

interceptor

Interceptors are used in AOP (Aspect-Oriented Programming) to intercept a method or field before it is accessed, and then add certain operations before or after it. Such as logs, security, etc.
The interceptor chain is to link the interceptors into a chain in a certain order. When an intercepted method or field is accessed, the interceptors in the interceptor chain are called in the order in which they were previously defined.
Generally, interceptor methods are implemented through dynamic proxies.

For example, it is
used to verify permissions, or to determine whether the user is logged in, or to determine whether the current time is the ticket purchase time like 12306.

the difference

  ①Interceptors are based on dynamic proxies, while filters are based on function callbacks.
  ②The interceptor does not depend on the servlet container, it is implemented through dynamic proxy, and the filter depends on the servlet container.
  ③Interceptor can be called before and after the method, before and after the exception, etc., while the filter can only be called once before and after the request.
  ④Interceptors can take advantage of dependency injection, so in Spring framework programs, interceptors are given priority.

1. The filter is a JavaEE standard, and it is performed in the form of function callback. After the request enters the container, it is preprocessed before entering the servlet, and post-processing is performed before the request is returned to the front end.

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("before...");
        chain.doFilter(request, response);
        System.out.println("after...");
    }

2. The interceptor is wrapped in the filter.

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }

Take a look at the execution order

 

 

 

Guess you like

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