Servlet、Filter、Listener、Interceptor

Reprint a blog: About the difference between Filter and Interceptorhttp:
//blog.csdn.net/zuoluoboy/article/details/19750699

First of all, the three roles of Servlet, Filter and Listener are defined in the JSP/Servlet specification, but they are not defined. The role of Interceptor, Interceptor is a role in some MVC frameworks. For example, in Struts2, Interceptor is used to intercept method calls in Action. Before the intercepted Action method is executed, the method in the response interceptor is executed first.
servlet, filter and listener are configured in web.xml, interceptor is not configured in web.xml, and the interceptor of struts is configured in struts.xml. Spring's interceptors are configured in spring.xml.


The loading order of servlet, filter, and listener has nothing to do with the order in which they are configured in the web.xml file. That is, the filter will not be loaded first because the filter is written in front of the listener: the order of loading them is: listener -> filter -> servlet
At the same time, there is such a configuration section: context-param, which is used to provide ServletContext with Key-value pair, that is, application context information. Our listener, filter, etc. will use the information in these contexts during initialization, so should the context-param configuration section be written before the listener configuration section? Actually the context-param configuration section can be written anywhere, so the real loading order is: context-param -> listener -> filter -> servlet


The loading order of context-param, listener, filter, and servlet is not affected by the order of configuration, but when there are multiple filters, the loading order between filters is affected. When each filter is initialized when the web container starts, it is initialized in the order in which the filter configuration section appears. When the requested resource matches multiple filter-mappings, the filter interception resource is in the order in which the filter-mapping configuration section appears. DoFilter is called in turn. () method.


===================================Filter================ =============================
filter filter was defined in Java Servlet Specification 2.3, before Java Servlet Specification 2.3 came out , there is no filter role.
The filter role is used to intercept the request before it reaches the servlet, and perform some preprocessing (such as encoding conversion, authorization verification) on the request. After processing, forward the request to the servlet or discard the request that does not meet certain rules, and no longer forward it to the servlet. When the servlet processes the request and returns the response to the browser, the filter intercepts the response, does some processing on the response, and then returns it to the browser. It can be seen that the role of filter is to help the servlet to do some pre-processing work (processing the request before the servlet) and post-processing (processing the response after the servlet). It is like the assistant of the servlet, but it It's not necessary, because the servlet works just fine without the filter before. It's just that with the help of filters, servlets can focus more on handling some core businesses.
Multiple filters can work together, and they use the chain of responsibility design pattern to work together. After a filter is processed, the next filter is called for processing. Each filter is responsible for processing different tasks, and these filters can be flexibly combined as needed. The order of filters is the order in which the filters are configured in web.xml.
The filter requires the support of the servlet container (tomcat), and the servlet container that can run the filter must implement the functions defined in the Java Servlet Specification Version 2.3. The servlet container programs the interface of javax.servlet.Filter, so the filter you write must directly or indirectly implement the interface of javax.servlet.Filter, otherwise the servlet container cannot interact with the filter you define (because in the When programming a container, the javax.servlet.Filter interface is programmed into the container code, and the container can only call the implementation class of the javax.servlet.Filter interface).




=======================================Listener============ =================================
The listener is a role that monitors and processes events. It adopts the observer mode, and only When the event registered on this listener
occurs , the listener will execute the event processing method. Examples of these events are: context loading events; session creation or destruction events; container, session or request attribute setting or removal events, etc.


The servlet2.4 specification provides 8 listener interfaces, which can be divided into three categories, as follows:
The first category: the listner interface related to servletContext (Application). Including: ServletContextListener, ServletContextAttributeListener
The second category: Listner interface related to HttpSession. Including: HttpSessionListner, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener;
the third category: Listener interfaces related to ServletRequest, including: ServletRequestListner, ServletRequestAttributeListener




=========================== ============Interceptor====================================== =====
Interceptor is a role in some MVC frameworks. For example, in Struts2, Interceptor is used to intercept method calls in Action. Before the intercepted Action method is executed, the method in the interceptor of the response is executed first. . Interceptor: Aspect-oriented programming, that is, calling a method before your service or a method, or calling a method after the method. For example, a dynamic proxy is a simple implementation of an interceptor. The filter is to intercept the request in front of the servlet, and the interceptor is to use the aspect-oriented programming technology to intercept the call method request in the internal Action of Struts.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326907947&siteId=291194637