Differences and connections between servlets, filters, listeners, interceptors
1. Concept
1.servlet: servlet is a Java application running server-side, with platform and protocol-independent features, and can dynamically generate web pages, which work in the middle layer between client requests and server responses.
2.filter: filter is a reusable code snippet that can be used to convert HTTP requests, responses and header information. Filter is not like a servlet. It cannot generate a request or response. It just modifies a request to a certain resource or modifies a response from a certain resource.
3. listener: listener, literally, it can be seen that listener is mainly used for listening only. Through the listener, you can listen to an execution action in a web server and make corresponding responses according to its requirements.
In simple terms, it is a functional component that automatically executes the code when the three objects of application, session, and request are created or when the modification and removal attributes are added to it.
4.interceptor: It is programming in tangential orientation, which 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 an interceptor. You can print out a string before you call the method (or do other business logic operations), you can also print out a string after you call the method, or even do business logic operations when you throw an exception.
5. servlet, filter, listener are configured into web.xml, interceptor is not configured into web.xml, struts interceptor is configured into struts.xml. The interceptor for spring is configured into spring.xml.
2. Loading order
The loading order of web.xml is: context-param -> listener -> filter -> servlet
3. Responsibilities
1.servlet:
(1) Create and return a complete html page containing dynamic content based on the nature of the client request
(2) Create a part of the html page (html fragment) that can be embedded in an existing html page
(3) Read hidden data sent by the client
(4) Read the display data sent by the client
(5) Communicate with other server resources (including database and java applications)
(6) Send hidden data to the client through status code and response header.
2.filter:
(1) Filter can preprocess user requests before a request reaches the servlet, or can also process http responses when leaving the servlet
(2) Before executing the servlet, first execute the filter program and do some preprocessing work for it.
(3) Modify the request and response according to the program needs
(4) Intercept the execution of the servlet after the servlet is called.
3.listener:
8 listener interfaces are provided in the servlet2.4 specification, which can be divided into three categories, as follows:
(1) Listne r interface related to servletContext. Including: ServletContextListener, ServletContextAttributeListener
(2) Listner interface related to HttpSession. Including: HttpSessionListner, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener
(3) Listener interfaces related to ServletRequest, including: ServletRequestListner, ServletRequestAttributeListener
4. Difference
1.servlet: The servlet process is short. After the url is transmitted, it is processed and then returned or turned to a page specified by you. It is mainly used to control before business processing.
2.filter: The process is threaded. After the url is transmitted, after checking, the original process can be kept downward and received by the next filter, servlet, etc., and after the servlet is processed, it will not continue to be passed downward.
The filter function can be used to keep the process going in the original way, or to dominate the process, while the servlet function is mainly used to dominate the process. Filter can be regarded as a supplement to the servlet (soft wipe).
Filter can be considered a "variant" of Servlet. It is mainly used to preprocess user requests, and can also post-process HttpServletResponse. It is a typical processing chain.
The difference between it and Servlet is that it cannot generate a response directly to the user.
The complete process is: Filter preprocesses user requests, then handes the requests to the servlet for processing and generates a response, and finally Filter post-processes the server response.
3. Matching rules
When a request is sent to the servlet container, the container will first subtract the requested url to the current application context path as the servlet mapping url. For example, I am visiting http://localhost/test/aaa.html (my application context is test).
The container will remove http://localhost/tes and use the remaining /aaa.html part to match the servlet mapping, that is, use the remaining part to match the url-pattern of the servlet configured in web.xml.
Note: There are certain rules for this mapping matching process, and each match will eventually match only one servlet. (This is different from filter)
servlet matching rules: When a servlet matches successfully, it will not be matched further
Precise path matching:
Example: For example, the url-pattern of servletA is /test, and the url-pattern of servletB is /*. At this time, if the url I access is http://localhost/test,
At this time, the container will first perform accurate path matching and find that /test is exactly matched by servletA, so call servletA and will not pay attention to other servlets.
Matching of the longest path:
Example: The url-pattern of servletA is /test/*, and the url-pattern of servletB is /test/a/*. When accessing http://localhost/test/a,
The container will select the servlet with the longest path to match, which is the servletB here.
Extension matching: If the last segment of the url contains an extension, the container will select the appropriate servlet based on the extension.
Example: url-pattern of servletA: *.action
4. servlets and filters are all aimed at urls and so on, while listeners are targeted at objects, such as the creation of sessions and the occurrence of session.setAttribute, and do something when such events occur.
Can be used to: Spring integrates Struts, inject attributes into Struts' action, implements web application timing tasks, statistics on the number of online users, etc.
5. Interceptor interceptor, similar to filter, but is configured in struts.xml, not in web.xml, and not for URLs, but for action. When the page submits an action,
Performing filtering operations is equivalent to the plug-in mechanism provided by struts1.x. It can be regarded as the former is the filter provided by struts1.x, and the interceptor is the filter provided by struts2.
Differences from filter:
(1) It is not configured in web.xml, but is configured in struts.xml, and is with action (2) The action itself can specify which interceptor to use to do things before receiving it
6. The difference and connection between filters and interceptors in struts2:
(1) Interceptor is provided by Struts2, while filters are provided by Servlet standard
(2) The interceptor intercepts the target method of target Action, while the filters target various web resources
(3) Interceptor is configured in struts.xml, while filter is configured in web.xml file
(4) Interceptors are organized together using an interceptor stack, while filters are linked together according to the intercepted resources. The order of execution is determined by their position in the configuration file.
(5) Interceptors are based on Java reflection mechanism, while filters are based on function callbacks.
(6) Filters depend on servlet containers, while interceptors do not depend on servlet containers.
(7) Interceptors can only work on Action requests, while filters can work on almost all requests.
(8) The interceptor can access objects in the Action context and value stack, but the filter cannot.
(9) During 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.
Thank you for reading, I hope it can help you. Thank you for your support for this site!