Servlet/filter/listener/interceptor differences and Connection

Source: Internet
Author: User

Since the work in the past two months has been relatively idle, I am also relatively "advanced", and I continue to learn about the difference between struts2 and struts1, why does struts1 use servlet while struts2 uses filter? What is the difference between Servlet and filter? So I looked at web. xml and found again, Servlet, filter, listener? Is there an interceptor? These concepts should have been mastered by beginners. Unfortunately, I am not good at basic learning and can only make up the courses now. So I had this blog.

Come on, there are many more places to make up the course. I didn't know they existed when I was a beginner. Haha.

The following describes the differences and links between the four concepts in the question:

1. Concepts

2. Lifecycle

3. Responsibilities

4. Execution Process

I. concept:

1. servlet: servlet is a Java application running on the server. It has the platform and Protocol features and can dynamically generate Web pages, it works on the intermediate layer of client requests and server responses.

2. filter: a reusable code snippet that can be used to convert HTTP request, response, and header information. Unlike servlet, filter cannot generate a request or response. It only modifies a request to a resource or modifies a response from a certain resource.

3. Listener: listener. It can be seen from the literal that Listener is mainly used for listening. Listener can be used to listen to an execution action on the Web server and respond accordingly. The common language is to automatically execute the code function component when the application, session, and request objects are created or deleted, or when the attributes are modified or deleted.

4. Interceptor: it is designed for Aspect-Oriented Programming, that is, calling a method before your service or method, or calling a method after the method, for example, a dynamic proxy is a simple implementation of the Interceptor. You can print a string (or perform other business logic operations) before calling the method, or print a string after calling the method, even when you throw an exception, you can perform business logic operations.

5. servlet, filter, and listener are configured in Web. XML, interceptor is not configured in Web. XML, and Struts interceptor is configured in struts. xml. Configure spring Interceptor to spring. xml.

Ii. lifecycle:

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 server ends or reloads the servlet. 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 or ended again.
(1) load: The servlet instance is loaded when the server is started;
(2) initialization: when the web server is started or the web server receives a request, or at a certain time between the two. The init () method is used for initialization;
(3) Call: for multiple accesses from the first time to the future, only the doget () or dopost () methods are called;
(4) destroy: Call the destroy () method when stopping the server to destroy the instance.

2. filter: (the three methods Init (), dofilter (), and destroy () of the filter interface of the javax. servlet package must be implemented. Empty implementations are also supported)
(1) load the filter instance when starting the server, and call the init () method to initialize the instance;
(2) Only the method dofilter () is called for processing each request;
(3) Call the destroy () method when stopping the server to destroy the instance.

3. Listener: similar to Servlet and filter

The loading sequence of Web. XML is: Context-param-> listener-> filter-> Servlet

4. Interceptor: Take the struts interceptor as an example. After loading struts. XML, initialize the corresponding interceptor. When an action request comes, the intercept method is called, and the server stops destroying interceptor.

Iii. Responsibilities

1. servlet:

Create and return a complete HTML page containing dynamic content based on the nature of the customer request;
Create a part of the HTML page (HTML segment) that can be embedded into an existing HTML page );
Reads hidden data from the client;
Reads the display data sent from the client;
Communicates with other server resources (including databases and Java applications;
Send hidden data to the client through the status code and response header.

2. filter:

Filter can pre-process user requests before a request arrives at the servlet, or process HTTP responses when leaving the servlet:
Before executing the servlet, first execute the filter program and perform some Preprocessing for it;
Modify the request and response as needed;
The servlet execution is intercepted after the servlet is called.

3. Listener: such as concept.

The servlet2.4 specification provides eight listener interfaces, which can be divided into three types:
Class 1: listner Interface related to servletcontext. Including: servletcontextlistener and servletcontextattributelistener
Class 2: listner Interface related to httpsession. Including: httpsessionlistner, httpsessionattributelistener, httpsessionbindinglistener, and httpsessionactivationlistener;
Category 3: Listener interfaces related to servletrequest, including servletrequestlistner and servletrequestattributelistener

4. Interceptor: similar to a filter, interceptor processes user requests and responses through layer-by-layer interception.


Note: The loading sequence of Web. XML is context-Param.
-> Listener-> filter-> servlet. After understanding the differences between these concepts, it is not difficult to theoretically load the sequence.


Iv. Differences:

1. The servlet process is short. After the URL is sent, it is processed, and then returned or directed to a specified page. It is mainly used for control before business processing.
2. The filter process is linear. After the URL is sent, after the check, the original process can be executed downward and received by the next filter and Servlet. After the servlet processes the process, will not continue to pass down. The filter function can be used to keep the process going in the original way or lead the process. The servlet function is mainly used to lead the process.
Filters can be used to filter character encoding, check whether users are logged in to filter, and disable page caching.
3. servlet and filter are for URLs and the listener is for object operations, such as session creation and session. setattribute occurs when such an event occurs.
It can be used for: Spring integrates struts, injects attributes into struts actions, implements scheduled tasks for Web applications, and counts the number of online users.
 
4, interceptor, similar to filter, but in struts. configured in XML, not in web. XML is not for URL, but for action. When the page submits an action, the filtering operation is equivalent to the plug-in mechanism provided by struts1.x, the former is the filter provided by struts1.x, and interceptor is the filter provided by struts2.
Different from filter: (1) It is not configured in Web. XML, but configured in struts. XML, with action
(2) The action can specify the Interceptor Used to do things before receiving them.

5. Differences and links between filters and interceptors in struts2:

(1) The Interceptor is based on the Java reflection mechanism, and the filter is based on function callback.
(2) The filter depends on the servlet container, and the interceptor does not depend on the servlet container.
(3) the interceptor can only act on Action requests, while the filter can act on almost all requests.
(4) the interceptor can access the objects in the Action context and value stack, but the filter cannot.
(5) In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.

V. Execution flowchart:

1. servlet:


2. filter:


3. Listener:

4. Interceptor:


From: http://blog.csdn.net/sundenskyqq/article/details/8549932

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.