Differences and connections between Java servlet, filter, Listener, Interceptor _java

Source: Internet
Author: User

Differences and connections between servlet, filter, Listener, Interceptor

First, the concept

1.servlet:servlet is a server-side Java application that has platform-and protocol-independent features and can dynamically generate Web pages that work in the middle tier of client requests and server responses.

2.filter:filter is a reusable code fragment that can be used to transform HTTP requests, responses, and header information. Unlike a servlet, filter does not produce a request or response, it simply modifies a request for a resource, or modifies a response from one.

3.listener: The listener, literally can see that listener is mainly used for listening. Listener allows you to listen to a single execution action on a Web server and respond accordingly to its requirements.

The popular language is that in Application,session,request, three objects are created extinct or added to them. The function component that automatically executes code when deleting attributes.

4.interceptor: It's in the aspect-oriented programming that you call a method before your service or a method, or call a method after the method.

For example, a dynamic proxy is a simple implementation of the Interceptor, before you call the method to print out the string (or do other business logic operations), you can call the method after you print out the string, even when you throw an exception to do business logic operation.

5.servlet, filter, listener is configured into Web.xml, interceptor is not configured into Web.xml, and struts interceptors are configured into Struts.xml. The Interceptor for spring is configured into Spring.xml.

Second, loading sequence

Web.xml loading order is: Context-param-> listener-> filter-> servlet

Iii. Responsibilities

1.servlet:

(1) Create and return a complete HTML page that contains dynamic content based on the nature of the customer request

(2) Create a subset of HTML pages (HTML fragments) that can be embedded in an existing HTML page

(3) Read the hidden data sent from the client

(4) Read the display data sent by the client

(5) Communication with other server resources (including database and Java applications)

(6) Send hidden data to the client through the status code and response headers.

2.filter:

(1) filter can preprocess user requests before a request arrives in the servlet, or process HTTP responses when leaving the servlet

(2) Before executing the servlet, execute the filter program first and do some preprocessing work

(3) Modify the request and response according to the program

(4) Intercepts execution of the servlet after the servlet is invoked.

3.listener:

The servlet2.4 specification provides 8 listener interfaces, which can be grouped into three categories, as follows:

(1) The Listne R interface associated with the ServletContext. Includes: Servletcontextlistener, Servletcontextattributelistener

(2) The Listner interface associated with the HttpSession. Include: Httpsessionlistner, Httpsessionattributelistener, Httpsessionbindinglistener, Httpsessionactivationlistener

(3) ServletRequest-related Listener interfaces, including: Servletrequestlistner, Servletrequestattributelistener

Iv. differences

The 1.servlet:servlet process is short, and after the URL is sent, it is processed and then returned or turned to a page of its own designation. It is primarily used to control the business before it is processed.

2.filter: The process is threading, after the URL comes, after checking, can keep the original process continues to execute downward, by the next filter, Servlet received, and the servlet processing, will not continue to pass down.

The filter function can be used to keep the process going the way it was, or to lead the process, and the servlet's function is primarily to lead the process. Filter can be viewed as a supplement to the servlet (wiping the butt).

Filter can be considered as a "variant" of the servlet, it is mainly used to preprocess user requests, or httpservletresponse, is a typical processing chain.

It differs from the servlet in that it does not generate a response directly to the user.

The complete process is: Filter to the user request preprocessing, then the request to the servlet to process and generate a response, and finally filter to the server response after processing.

3. Matching rules

When a request is sent to the servlet container, the container first subtracts the requested URL from the current application context as the servlet's mapped URL, for example, I am accessing http://localhost/test/aaa.html (my application context is test).

The container will remove the http://localhost/tes and take the rest of the/aaa.html part for a servlet mapping match, which is to match the remainder with the url-pattern of the servlet configured in Web.xml.

Note: This mapping matching process has certain rules, and each match will eventually match only one servlet. (This is different from filter)

servlet matching rule: When a servlet match succeeds, it doesn't go down to match.

Exact matching of paths:

Example: For example Servleta's url-pattern for/TEST,SERVLETB url-pattern for/*, this time, if I access the URL for http://localhost/test,

This time the container will be the exact path matching, found that/test exactly by Servleta Exact match, then go to call Servleta, will not ignore the other servlet.

Match for Longest path:

Example: Servleta's Url-pattern is/test/*, and Servletb Url-pattern is/test/a/*, when you visit http://localhost/test/a,

The container chooses the longest servlet in the path to match, which is the SERVLETB here.

extension Matching : If the last paragraph of the URL contains an extension, the container will select the appropriate servlet based on the extension.

Example: Servleta's Url-pattern:*.action

4.servlet,filter are all for URLs, and listener are for object manipulation, such as session creation, Session.setattribute, and doing something when such an event occurs.

Can be used for: spring consolidation struts, action injection attributes for struts, implementation of Web application timing tasks, online headcount statistics, etc.

5.interceptor Interceptor, similar to filter, but configured in Struts.xml, not in Web.xml, and not for URLs, but for action, when the page submits action,

Filtering operations, equivalent to the struts1.x provided by the plug-in mechanism, can be seen as the former is struts1.x with the filter, and interceptor is the STRUTS2 provided filter.

differs from filter point:

(1) is not configured in the Web.xml, but in the Struts.xml to complete the configuration, with the action
(2) can be specified by the action itself which interceptor to work before receiving

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

(1) The Interceptor is provided by the Struts2, and the filter is provided by the Servlet standard

(2) Interceptor intercept target Action target method, and filter for various web resources

(3) The interceptor is configured in the Struts.xml, and the filter is configured in the Web.xml file

(4) Interceptors are organized using interceptor stacks, and filters are linked according to the intercepted resource, and they determine the sequence of execution in their location in the configuration file

(5) The Interceptor is based on the Java reflection mechanism, and the filter is based on the function callback.
(6) The filter relies on the servlet container, and the interceptor does not depend on the servlet container.
(7) The interceptor can only function on the action request, and the filter works on almost any request.
(8) The interceptor can access the action context, the object in the value stack, and the filter cannot.
(9) in the life cycle of the action, the interceptor can be invoked multiple times, and the filter can only be invoked once when the container is initialized.

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.