JavaWeb-Interceptor, Filter, Listener Difference and Execution Order

One : Interceptor: Aspect-oriented programming is to call a method before your service or a method, or call a method after the method. For example, dynamic proxy is a simple implementation of interceptor, and pre-notification in aop of springmvc and post notification.

Two : Filter: In the java web, the request and response you pass in 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 request of login.do is filtered out if the user is not logged in),
or the character set is uniformly set before the action of the incoming servlet or struts, or some illegal characters are removed. Mainly to reduce server load and reduce pressure

The difference between interceptors and filters

1. The interceptor is based on the reflection mechanism of java, while the filter is based on the function callback
. 2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container
. 3. The interceptor can only work on action requests, while filtering The interceptor can work on almost all requests
4. The interceptor can access the objects in the action context and value stack, while the filter cannot
. 5. In the life cycle of the action, the interceptor can be called multiple times, while the filter only It can be called once when the container is initialized.
In the life cycle of the action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized.
Execution sequence: before filtering - before intercepting - action processing - after intercepting - After filtering.
I personally think that filtering is a horizontal process. First, filter the content submitted by the client (for example, the processing that users who are not logged in cannot access internal pages);
after the filtering is passed, the interceptor will check the verification of the data submitted by the user, and do some preliminary data. Processing, and then send the processed data to the corresponding Action; after the
Action processing is completed and returned, the interceptor can also do other processes, and then return to the subsequent operations of the filter.

Three : Listener: Servlet's listener Listener, which is
   a server-side program that implements the javax.servlet.ServletContextListener interface. It is also started with the startup of the web application, only initialized once, and destroyed when the web application stops.
The main functions are: to do some initialization content addition work, to set some basic content, such as some parameters or some fixed objects, etc.

Four : Startup sequence
Listener > Filter > Interceptor
I remember this way: I received an order from the superior, asking to monitor the phone, filter out terrorists, and then intercept their terrorist attacks.


转自
https://blog.csdn.net/qq_25497867/article/details/53482304

Guess you like

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