Servlet/filter/listener/interceptor Difference and Contact

Source: Internet
Author: User

Because of the recent two months work relatively leisurely, the individual also more "progressive", the use of work free time, also continue to learn, one day suddenly remembered the difference between struts2 and struts1, found why Struts1 to use the servlet, and struts2 to use filter? What is the difference between a servlet and a filter? So looked at web. XML, and Found, Yi, servlet, filter, listener? There's a interceptor? For these concepts, it should be a beginner to grasp the east, but I do not have the basic science, can only be the present lesson. So there's this blog post.

Take your time, there are a lot of places you need to make up your own lessons. They didn't know they existed when they were beginners. Oh.

The following is a few aspects of the topic of the difference between the four concepts and links:

1. Concept

2. Life cycle

3. Responsibilities

4, the implementation process

First, the concept:

1, Servlet:servlet is a Java application running on the server side, with platform and protocol-independent features, and can dynamically generate Web pages, it works in the client request and server response to the middle tier.

2. Filter:filter is a reusable code fragment that can be used to convert HTTP request, response, and header information. Filter is not like a servlet, it cannot produce a request or response, it simply modifies a request to a resource, or modifies a response from one.

3, listener: listener, literally can be seen listener mainly used for monitoring. With listener, you can listen to one of the execution actions in the Web server and respond accordingly to its requirements. A popular language is a functional component that automatically executes code when a application,session,request three object is created or is added to a modified delete property.

4, Interceptor: is in aspect-oriented programming, is in your service or a method, before calling a method, or call a method after the method, such as a dynamic proxy is the simple implementation of the Interceptor, before you call the method to print out the string (or other business logic operation), You can also print out a string after you invoke the method, even if you do business logic when you throw an exception.

5, servlet, filter, listener is configured in Web. XML, Interceptor is not configured in Web. XML, the Struts interceptor is configured in Struts.xml. The spring interceptor is configured in the Spring.xml.

Second, life cycle:

1. The Servlet:servlet life cycle begins when it is loaded into the Web server's memory and ends when the Web server terminates or re-loads the servlet. Once a servlet is mounted on a Web server, it is typically not removed from the Web server's memory until the Web server shuts down or ends again.
(1), Loading: Load the servlet instance when starting the server;
(2), initialization: When the Web server starts or when the Web server receives a request, or at some point between the two starts. Initialization work has init () method responsible for execution completion;
(3), call: From the first to the next multiple visits, are only called doget () or Dopost () method;
(4), destroy: Call the Destroy () method when stopping the server, destroy the instance.

2, Filter: (Be sure to implement the Javax.servlet packet filter interface of three methods Init (), DoFilter (), Destroy (), empty implementation also line)
(1), load the instance of the filter when the server is started, and call the Init () method to initialize the instance;
(2), each request is only called Method Dofilter () for processing;
(3), stop the server when the Destroy () method is called, destroy the instance.

3. Listener: Similar to servlet and filter

The load order for Web. XML is: servlet, listener, Context-param,

4, Interceptor: With Struts interceptor as an example, after loading the struts.xml, initialize the corresponding interceptor. When the action request comes up, The Intercept method is called and the server stops destroying interceptor.

Iii. Responsibilities

1. servlet:

Create and return a complete HTML page that contains dynamic content based on the nature of the customer request;
Create a portion of an HTML page (HTML fragment) that can be embedded in an existing HTML page;
Read hidden data sent by the client;
Read the display data sent by the client;
Communicates with other server resources, including databases and Java applications;
Sends hidden data to the client through a status code and a response header.

2. Filter:

Filter can preprocess user requests before a request arrives at the servlet, or it can handle HTTP responses when leaving the servlet:
Before executing the servlet, the filter program is executed first, and some preprocessing work is done for it.
Modify requests and responses as required by the program;
Intercepts the execution of the servlet after the servlet is invoked

3, listener: responsibilities such as concept.

The servlet2.4 specification provides 8 listener interfaces, which can be divided into three categories, as follows:
First class: Listner interface related to ServletContext. Includes: Servletcontextlistener, Servletcontextattributelistener
Class II: Listner interfaces related to HttpSession. Including: Httpsessionlistner, Httpsessionattributelistener, Httpsessionbindinglistener, httpsessionactivationl Istener;
Category III: Listener interfaces related to ServletRequest, including: Servletrequestlistner, Servletrequestattributelistener

4, Interceptor: very similar to the filter, through layer interception, processing the user's request and response.

Note: Web. XML is loaded in the following order: Context-param, Listener, Filter-a servlet. After understanding the differences between these concepts, it is not difficult to theory this loading order.

Four, several differences:

The 1,servlet process is short, and after the URL is transmitted, 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 flow is linear, after the URL, after the inspection, can keep the original process continues to execute downward, by the next filter, Servlet received, etc., and the servlet will not continue to pass down after processing. The filter function can be used to keep the process going as it is, or to dominate the process, and the servlet's functionality is primarily used to dominate the process.
Filter can be used for character encoding filtering, detection of user login filtering, prohibit page cache, etc.
3, Servlet,filter are for URLs and so on, and listener is an object-oriented operation, such as the creation of a session, the occurrence of session.setattribute, and doing something when such an event occurs.
Can be used to: spring consolidates struts, injects properties for struts action, implements Web application timed tasks, online demographics, etc.

4,interceptor interceptors, similar to filter, but configured in Struts.xml, not in Web. XML, and not for URLs, but for action, when the page commits the action, Equivalent to the plug-in mechanism provided by struts1.x, it can be seen as the former is Struts1.x's own filter, and interceptor is the filter provided by STRUTS2.
Unlike the filter: (1) is not configured in Web. config, but is configured in Struts.xml, with action
(2) Which interceptor can be specified by the action to work before receiving

The difference and connection between filters and interceptors in 5,STRUTS2:

(1), interceptors are based on the Java reflection mechanism, and filters are based on function callbacks.
(2), the filter relies on the servlet container, while the interceptor is not dependent on the servlet container.
(3), interceptors only work on action requests, while filters can work on almost any request.
(4), the interceptor can access the action context, the object in the value stack, and the filter cannot.
(5), in the life cycle of the action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized.

Five, the implementation process diagram:

1. servlet:

2. Filter:

3, Listener:

4, Interceptor:

Servlet/filter/listener/interceptor Difference and Contact

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.