Interview question summary: the difference between filter and interceptor


The difference between filters and interceptors

  ①The interceptor is based on the reflection mechanism of java, and the filter is based on the function callback.
  ②The interceptor does not depend on the servlet container, the filter depends on the servlet container.
  ③Interceptors can only work on action requests, and filters can work on almost all requests.
  The interceptor can access the objects in the action context and value stack, but the filter cannot.
  ⑤ In the action life cycle, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized.
    ⑥The interceptor can obtain each bean in the IOC container, but the filter is not good. This is very important. Injecting a service in the interceptor can call business logic.

1. The filter is a JavaEE standard, which uses a function callback method. After the request enters the container, it is pre-processed 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...");
    }

chain.doFilter (request, response); The call of this method serves as a watershed. In fact, the doService () method of calling Servlet is performed in chain.doFilter (request, response); this method .

 


2. The interceptor is wrapped in a 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");
    }

a.preHandle () This method is executed in the previous step of the filter's chain.doFilter (request, response) method, that is, in [System.out.println ("before ...")] [chain.doFilter (request , response)].

After the b.preHandle () method, before the return ModelAndView, you can manipulate the ModelAndView content of the Controller.

The c.afterCompletion () method is executed before the filter returns to the front end, that is, between [chain.doFilter (request, response)] [System.out.println ("after ...")].

 

3. The mechanism of SpringMVC is that the same Servlet distributes requests to different Controllers. In fact, this step is performed in the Servlet's service () method. So the execution order of filters, interceptors, service () methods, and dispatc () methods should be like this, roughly drawing a picture: In fact, it is very good to test, write a filter, an interceptor, and then in these methods All add a breakpoint, and all the way to F8 came to a conclusion.
 

Summary: The interceptor function is indeed very useful in request authorization identification. In this project I participated in, each request of a third-party remote call needs to participate in the identification, so it is very convenient to do so, and he is very independent. Logic, this makes the business logic code very clean. Like other functions of the framework, the principle is very simple, and it is also very simple to use. Looking at the source code of SpringMVC, it is actually relatively easy to understand.

  In our project, only the preHandle method is used, but nothing else. The framework provides an adapter class HandlerInterceptorAdapter that has implemented the interceptor interface. Inherit this class and rewrite the methods that need to be used. Code, this way is reflected in many places in Java.
 

"The Return of the King of Java Web Integration Development (JSP + Servlet + Struts + Hibernate + Spring)":

 

reference:

https://blog.csdn.net/chenleixing/article/details/44573495#reply

https://www.cnblogs.com/dreamroute/p/4198087.html?utm_source=tuicool%EF%BC%9A

https://www.cnblogs.com/alilcu/p/8073744.html

https://blog.csdn.net/zxd1435513775/article/details/80556034

 

 

 

 

 

 

 

 

 

Published 162 original articles · won 30 · 90,000 views +

Guess you like

Origin blog.csdn.net/ScorpC/article/details/100131843