The difference between Spring interceptors and filters

Interceptor refers to an enhanced usage scenario that completes the function by uniformly intercepting requests sent from the browser to the server
: Solving the common problems of requests (garbled characters, permission verification problems)

1. SpringMVC can solve the problem of garbled characters by configuring filters
2. The working principle of the interceptor is very similar to that of filtering, and it can also be used to solve the problem of garbled


characters. Implementation of the
interceptor 1. Write the interceptor class to implement the HandlerInterceptor interface.
Three methods that must be implemented are
preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) 
(first Step: Does the call need to intercept the current request before the request is processed? If it returns false, the request will be terminated, and if true, the request will continue. Object arg2 represents the target method instance of the intercepted controller)

When entering an interceptor in the interceptor chain and executing the preHandle method

 

postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) 
(Step 2: call after the request is processed ModelAndView arg3 refers to the object that will be presented on the web page, you can modify this object to achieve different roles to jump to different directions The webpage or different message prompts)

afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) 
(The third step: It is generally used to close the stream, resource connection, etc. after the request is completed, which is rarely used)

2. Use the interceptor Register in the SpringMVC framework


 

    <!-- Access to static resource files -->
    <!-- <mvc:resources mapping="/images/**"  location="/images/"/>
    <mvc:resources mapping="/css/**"  location="/css/" />
    <mvc:resources mapping="/js/**"  location="/js/" />
    <mvc:resources mapping="/favicon.ico" location="favicon.ico" /> -->  
    <!--Configure interceptors, multiple interceptors, execute sequentially -->
    <mvc:interceptors>
           <mvc:interceptor>
                   <!--  
                       /** means all folders and subfolders inside
                       /* are all folders, no subfolders
                       / is the root directory of the web project
                     -->
                  <mvc:mapping path="/**" />
                   <!-- Addresses to be excluded from interception-->  
                   <!--  <mvc:exclude-mapping path="/userController/login"/>  -->
                   <bean id="commonInterceptor" class="org.shop.interceptor.CommonInterceptor"></bean>  <!--This class is our custom Interceptor --> 
          </mvc:interceptor> 
          <!-- When setting multiple interceptors, call the preHandle method in order, and then call the postHandle and afterCompletion methods of each interceptor in reverse order -->
   </mvc:interceptors>

 


Interceptor and filter comparison
①Interceptor is based on Java 's reflection mechanism, while filter is based on function callback.
② The interceptor does not depend on the servlet container, and the filter depends on the servlet container.
③Interceptors can only work on action requests, while filters can work on almost all requests, and can work on requested resources
④Interceptors can access objects in the action context and value stack, while filters Can not access.
⑤ In the life cycle of action, the interceptor can be called multiple times, while 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 cannot. This is very important. Injecting a service into the interceptor can call the business logic.



 

Guess you like

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