Summary of the difference between JavaWeb interceptor (interceptor) and filter (filter)

1. Difference

  1. Realization principle
  2. Use and scope of application
  3. scenes to be used
  4. trigger timing
  5. Order of requests

2. Specific Explanation

2.1 Implementation principle

Filter Filter : Based on the function, that is, the method callback, to complete the interception and release operations.
Interceptor (interceptor) : The interceptor is implemented through the Java reflection mechanism (dynamic proxy).

2.2 Use and scope of application

Filter Filter : filter is defined by the earliest Java-based Servlet specification, so filter depends on tomcat and is basically only used for JavaWeb project
Interceptor (interceptor) : a separate component provided by spring, managed by spring and has nothing to do with tomcat It can be used alone without relying on tomcat.
Interceptors are not only used for web but also for Application.Swing etc.

2.3 Usage Scenarios

Filter : It is more used for request interception, basic processing of requests and other unified basic functional operations, but it is not impossible to check permissions.
Interceptor : It is more suitable for the business operations of the system itself, such as permissions, logs, business system validity checks, whether it is available, and so on. But can also be used for general request and response interception.

2.4 Trigger Timing

Filter Filter : Start before the request enters the Servlet and end after the Servlet processing is completed.
Interceptor (interceptor) : After the request enters the Servlet, before entering the controller, the controller ends after rendering the view.

2.5 Request order

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44131922/article/details/131675481