The differences and connections between Java servlet filter listener interceptor

  • 2020-05-16 06:59:00
  • OfStack

The differences and connections between servlet, filter, listener, interceptor

1. The concept

1.servlet: servlet is a kind of java application running on the server side, which is independent of platform and protocol, and can dynamically generate web pages. It works in the middle layer between client requests and server responses.

2.filter: filter is a reusable code fragment that can be used to transform HTTP request, response, and header information. Unlike Servlet, Filter does not generate a single request or response; it simply modifies a request to a resource or modifies a response from a resource.

3.listener: listener, it can be seen from the text that listener is mainly used for listening only. Through listener, you can listen to one of the execution actions in the web server and make corresponding responses according to its requirements.

In layman's terms, application, session, and request are functional components that automatically execute code when three objects are created or added to them to modify or delete properties.

4.interceptor: programming in the aspect, that is, calling one method in front of your service or one method, or calling one method after the method.

For example, a dynamic proxy is a simple implementation of an interceptor that prints a string before you call a method (or does other business logic operations), prints a string after you call a method, or even does business logic operations when you throw an exception.

5.servlet, filter, listener are configured to web.xml, interceptor is not configured to web.xml, struts's interceptors are configured to struts.xml. The interceptor for spring is configured to spring.xml.

2. Loading sequence

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

3. The responsibility

1.servlet:

(1) create and return a complete html page containing dynamic content based on the nature of the customer request

(2) create part 1 of the html page (html fragment) that can be embedded into the existing html page

(3) read the hidden data sent by the client

(4) read the display data sent by the client

(5) communicate with other server resources, including databases and java applications

(6) send hidden data to the client through the status code and response header.

2.filter:

(1) filter can preprocess user requests before one request arrives at servlet, or process http responses when leaving servlet

(2) before executing servlet, first execute filter program and do some pre-processing for it

(3) modify the request and response according to the needs of the program

(4) intercept the execution of servlet after servlet is called.

3.listener:

The servlet2.4 specification provides eight listener interfaces, 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 interface related to ServletRequest, including: ServletRequestListner, ServletRequestAttributeListener

4. The difference between

1.servlet: the servlet process is short. When url is sent, it is processed, and then it returns or goes to a page specified by it. It is mainly used for control before business processing.

2.filter: the process is threaded. After the transmission of url and the inspection, the original process can be kept to continue to be executed downward, which is received by the next filter, servlet, etc., while after the processing of servlet, the process will not continue to be transmitted downward.

The filter function can be used to either keep the process going the way it was or to lead the process, whereas the servlet function is mainly used to lead the process. Think of Filter as a complement to servlet.

Filter can be considered as a "variant" of Servlet. It is mainly used for preprocessing user requests, and also for post-processing HttpServletResponse, which is a typical processing chain.

It differs from Servlet in that it does not generate responses directly to the user.

The complete process is: Filter preprocesses the user request, then passes the request to Servlet for processing and generates the response, and finally Filter post-processes the server response.

3. Match rules

When a request is sent to the servlet container, the container will first request url minus the path of the current application context as the mapping url servlet, such as my visit is http: / / localhost test/aaa html application context is test (I),

Container will http: / / localhost tes take out, the remaining/aaa html servlet map matching part to do, is to take the rest of the and web. xml configured in servlet url - pattern match.

Note: this mapping matching process has 1 set of rules, and each match ends up matching only 1 servlet. (this 1 is different from filter)

servlet matching rule: once the 1 hang servlet matches successfully, it will no longer match in the following direction

Exact path matching:

Example: such as servletA url - pattern/test servletB url - pattern / *, at this time, if I visit url http: / / localhost test,

At this point, the container will do the exact path matching first, and find that /test is exactly matched by servletA, so it will call servletA and ignore the other servlet.

Longest path matching:

Example: servletA url - pattern/test / *, and servletB url - pattern test/a / *, at this time access http: / / localhost test a,

The container will select servlet with the longest path to match, which is servletB here.

Extension matching: if the last section of url contains an extension, the container will select the appropriate servlet based on the extension.

Example: url-pattern: *.action

4.servlet,filter are all for url and so on, while listener is for the operation of objects, such as the creation of session, the occurrence of session.setAttribute, to do something when such an event occurs.

Available for: Spring integration Struts, Struts action injection attributes, web application timing task implementation, online population statistics, etc

5.interceptor interceptor, similar to filter, but configured in struts.xml, not web.xml, and not for URL, but for action, when the page submits action,

The filtering operation is equivalent to the plug-in mechanism provided by struts1.x, which can be regarded as struts1.x built-in filter and interceptor filter provided by struts2.

Differences from filter:

(1) it is not configured in web.xml, but in struts.xml, starting at 1 with action
(2) action can specify which interceptor to use before receiving

6. Differences and connections between filters and interceptors in struts2:

(1) the interceptor is provided by Struts2, while the filter is provided by the Servlet standard

(2) the interceptor intercepts the target Action target method, while the filter targets various web resources

(3) the interceptor is configured in struts.xml, while the filter is configured in the web.xml file

(4) the interceptor USES the interceptor stack to organize at 1, while the filter is connected at 1 according to the intercepted resource, and the execution order is determined by their location in the configuration file

(5) the interceptor is based on the java reflection mechanism, while the filter is based on the function callback.
(6) the filter depends on the servlet container, while the interceptor does not depend on the servlet container.
(7) the interceptor can only work on Action requests, while the filter can work on almost all requests.
(8) interceptors can access objects in the Action context and value stack, but filters cannot.
(9) in the lifetime 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 to help you, thank you for your support of this site!


Related articles: