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. Concept 2. Life Cycle 3. Responsibility 4. Execution Process 1. 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 in the middle layer between 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 lifecycle of a servlet starts when it is loaded into the memory of the web server and ends when the web server is terminated or reloaded into 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: The Servlet instance is started when the web server is started or when the web server receives a request, or at a certain time between the two. The initialization is completed by the init () method. (3) Call: only the doGet () or doPost () method is called for multiple accesses from the first time to the future; (4) destroy: Call the destroy () method when stopping the server to destroy the instance. 2. filter: (javax must be implemented. the three methods init (), doFilter (), and destroy () of the Filter interface of the servlet package are also empty.) (1). When the server is started, the Filter instance is loaded, the init () method is called to initialize the instance. (2) Only the doFilter () method is called for processing each request. (3) The destroy () method is called when the server is stopped, destroys an instance. 3. listener: similar to servlet and filter web. the xml loading sequence is context-param-> listener-> filter-> servlet 4. interceptor: the struts interceptor is loaded as an example. initialize the corresponding interceptor after xml. 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 customer requests; create an html page (html segment) that can be embedded in an existing html page; read hidden data from the client; read the display data sent from the client; communicates with other server resources (including databases and java applications), and sends hidden data to the client through the status code and response header. 2. filter: the 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, the filter program is first executed, perform some Preprocessing for it. Modify the request and response according to the program's needs. After the servlet is called, it intercepts the servlet's execution. 3. listener: Concept of responsibility. The servlet2.4 specification provides eight listener interfaces, which can be divided into three types: the First Class: listner interfaces related to servletContext. Includes ServletContextListener and ServletContextAttributeListener. Class 2: Listner Interface related to HttpSession. Including HttpSessionListner, Listener, HttpSessionBindingListener, and HttpSessionActivationListener. Category 3: Listener interfaces related to ServletRequest, including ServletRequestListner, Listener 4, and interceptor. They are very similar to filters and interceptor, process user requests and responses. 4. Differences: 1. the servlet process is short. After a 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, disable page caching, etc. 3. servlet and filter are all for URLs, while 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 for 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) not on the web. configured in xml, but in struts. complete the configuration in xml, and be associated with the action (2) The action can specify which interceptor is used to do the work before receiving it. 5. Differences and relationships between filters and interceptors in struts2: (1) the Interceptor is based on the java reflection mechanism, and the filter is based on the 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. 5. Execution flowchart: 1. servlet: 2. filter: 3. listener: 4. interceptor:

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.