The difference between listener Listener + filter filter + interceptor interceptor in JavaWeb

The difference between listener Listener + filter filter + interceptor interceptor in JavaWeb

If you look at the entire project, the execution process of a servlet request becomes like this context-param–>listener–>filter–>servlet–>interceptor (referring to the interceptor)

1. Concept

Context-param: It is some configuration that needs to be initialized , put it into context-param, so that it is monitored by the listener (here specifically refers to org.springframework.web.context.ContextLoaderListener), and then loaded;

Listener (listener): It is to monitor the project , and it can perceive the initialization and attribute changes of request (request domain), session (session domain) and applicaiton (application program);

Filter (filter): It is to filter the request . It filters the request after the listener and before the servlet;

servlet: It is the container that processes the request and response . It is executed after the filter. Part of the servlet is the controller layer (marked as servlet_2), which also includes the rendering view layer (marked as servlet_3) and some processing parts of the system before entering the controller. (servlet_1), and we mark the moment when the servlet starts as servlet_0, and the moment when the servlet ends as servlet_4.

Interceptor (interceptor): It is to intercept the request and return . It acts inside the servlet. Specifically, there are three places:
insert image description here

the difference

1. The servlet process is short. After the url is transmitted, it is processed, and then returns or redirects to a page specified by itself. It is mainly used to control before business processing.

2. The filter process is linear. After the url is sent and checked, the original process can continue to be executed downwards, and it can be received by the next filter, servlet, etc., but after the servlet processes, it will not continue to be passed down. The filter function can be used to keep the process going on in the original way, or to lead the process, while the servlet function is mainly used to lead the process.

filter can be used to filter character encoding, detect whether the user is logged in, prohibit page caching, etc.

3. servlets and filters are all for urls and the like, while listeners are for object operations , such as session creation, session.setAttribute occurrence, and do something when such an event occurs.

It can be used to: Spring integrates Struts, injects attributes into Struts actions, implements timing tasks for web applications, and counts online users, etc.;

4, interceptor interceptor, similar to filter, but not in web.xml, and not for url but for action , when the page is submitted, the filtering operation is performed;

Principle of use:
After the whole process is clear, then it is their respective use. Before using it, there should be a usage rule. Why do you say this, because some functions, such as judging whether a user is logged in, can be used either as a filter or as an interceptor. Which one is reasonable? So if there is a principle, it will be more reasonable to use. In fact, this principle exists:

Comparing the entire project process to a river, the function of the monitor is to be able to hear all the sounds in the river, the filter is to filter out the fish in it, and the interceptor is to intercept some of the fish in it and mark it. So when you need to monitor some information in the project and do not need to make changes to the process, use the listener; when you need to filter out some of the information and leave only part of it, use the filter; when you need to change the process , Use interceptors when making related records.

The interceptor will be described in detail. The previous article said that the Spring configuration file should scan the service layer and below, and the SpringMvc configuration file should scan the controller layer; if we want to log in the service layer, we can use spring aop Features, you can configure aspect in spring.xml, so if you want to log in the controller layer, how should you configure it in SpringMvc.xml?

At this time, an interceptor is needed. It is actually an implementation of aop (aop itself is an idea), and this implementation is essentially the same as aspect, but it does more things. Of course, we can use it in SpringMvc. Aspect is also configured in xml, but now there is a better implementation, why not use it.

life cycle

1.servlet

The life cycle of a servlet begins when it is loaded into the memory of the web server and ends when the web service is terminated or the servlet is reloaded;

Once the servlet is loaded into the web server, it is generally not deleted from the web server memory; until the web server is closed;

load: load the instance of the servlet when starting the server;

Initialization: When the web server receives a request, or starts at some point in between, it calls init()

Call: From the first visit to the subsequent visits, only the doGet() or dopost() method is called;

Destroy; when the server is stopped, the destroy() method is called to destroy the instance;

2.filter

Need to implement the three methods init(), doFilter(), destroy() of the Filter interface of the javax.servlet package;

Load: Load the instance of the filter when starting the server, and call the init() method;

Call: only call the method doFilter() for each request;

Destruction: Call the destroy() method before the server is closed to destroy the instance;

3.listener

The loading order of web.xml is: context-param->listener->filter->servlet

4.interceptor

After loading the configuration file, initialize the interceptor. When there is a request for action, call the interceptor method, and finally destroy it according to the server stop;

Implementation process:

insert image description here

insert image description here

insert image description here

insert image description here
Reference blog: https://blog.csdn.net/Jintao_Ma/article/details/52972482
https://www.cnblogs.com/heyanan/p/9591670.html

Guess you like

Origin blog.csdn.net/weixin_37766296/article/details/112392968