Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences and connections between servlet/filter/listener/interceptor?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the differences and connections of servlet/filter/listener/interceptor?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference and relation of servlet/filter/listener/interceptor

This paper expounds the differences and relations of the four concepts in the title from the following aspects:

1. Concept 2, life cycle 3, responsibilities 4, distinction 5, implementation flowchart 1, concept:

Servlet:servlet is a server-side java application, which is independent of platform and protocol, and can generate web pages dynamically. It works in the middle layer between client request and server response.

Filter:filter is a reusable code snippet that can be used to transform HTTP requests, responses, and header information. Unlike Servlet, Filter cannot produce a request or response, it just modifies a request for a resource, or modifies a response from a resource.

Listener: listener. Literally, you can see that listener is mainly used for listening. Through listener, you can listen to an execution action in the web server and respond accordingly according to its requirements. The popular language is the functional component that automatically executes the code when the three objects of application,session,request are created or disappeared or when modified and deleted properties are added to it.

Interceptor: aspect-oriented programming, that is, calling a method before your service or a method, or calling a method after a method. For example, a dynamic proxy is a simple implementation of the interceptor, printing a string before you call the method (or doing other business logic operations), or even doing business logic operations after you call the method, or even when you throw an exception.

Servlet, filter, and listener are configured in web.xml, interceptor is not configured in web.xml, and struts's interceptor is configured in struts.xml. The interceptor for spring is configured in spring.xml.

Second, life cycle:

The life cycle of a servlet:servlet begins when it is loaded into the memory of the web server and ends when the web server terminates or reloads the servlet. Once servlet is mounted on a web server, it is generally not removed from the memory of the web server until the web server is shut down or reloaded.

(1)。 Load: load an instance of Servlet when you start the server (if you set a value greater than 0, tomcat will create a servlet at startup, if this property is not set, it will be created on the first visit)

(2)。 Initialization: starts when the web server starts or when the web server receives the request, or at some point in between. The initialization work is performed by the init method; (the init method is executed only once)

(3)。 Call: from the first to the following multiple visits, only the doGet () or doPost () method is called

(4)。 Destroy: when the server is stopped, the destroy () method is called to destroy the instance.

Image.png

Filter: (be sure to implement the three methods init (), doFilter (), destroy () of the Filter interface of the javax.servlet package, or an empty implementation is fine)

(1)。 Load the instance of the filter when you start the server and call the init () method to initialize the instance

(2)。 Only the method doFilter () is called for processing each request.

(3)。 The destroy () method is called when the server is stopped to destroy the instance.

Listener: similar to servlet and filter

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

Interceptor: take the interceptor of struts as an example. After loading the struts.xml, initialize the interceptor. The intercept method is called when the action request comes, and the server stops destroying the interceptor.

III. Responsibilities

Servlet:

Create and return a complete html page that contains dynamic content based on the nature of the customer request

Create a partial html page (html fragment) that can be embedded in an existing html page

Read the hidden data sent by the client

Read the display data sent by the client

Communicate with other server resources, including database and java applications

Send hidden data to the client through the status code and the response header.

Filter:

Filter can preprocess user requests before a request arrives at servlet, or it can process http responses when leaving servlet

Before executing servlet, first execute the filter program and do some preprocessing work for it

Modify requests and responses according to the needs of the program

Intercept the execution of servlet after servlet is called

Listener: responsibilities are concepts.

Eight listener interfaces are provided in the servlet2.4 specification, which can be divided into three categories, as follows:

The first category: listner interface related to servletContext.

Including: ServletContextListener, ServletContextAttributeListener

The second category: Listner interface related to HttpSession.

Including: HttpSessionListner, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener

The third category: ServletRequest-related Listener interfaces

Including: ServletRequestListner, ServletRequestAttributeListener

Interceptor:

Much like a filter, it handles user requests and responses through layers of interception.

Fourth, there are several differences:

The servlet process is short, and after the url comes in, it is processed, and then returned or redirected to a self-specified page. It is mainly used to control before business processing.

The filter process is linear. After the url is sent and checked, the original process can continue to be executed downwards and received by the next filter, servlet, etc., while after servlet processing, it will not continue to be passed down. The filter function can be used to keep the process going the way it was, or to dominate the process, while the function of servlet is mainly used to dominate the process.

Filter can be used to filter character encodings, detect whether users log in, and disable page caching.

Servlet,filter is for url and the like, while listener is for object operations, such as the creation of session, the occurrence of session.setAttribute, to do something when such an event occurs.

It can be used to integrate Struts with Spring, inject attributes into action of Struts, realize timed tasks of web application, count the number of people online, etc.

Interceptor interceptor, similar to filter, but configured in struts.xml, is not in web.xml, and not for URL, but for action. When the page submits action, it carries out filtering operation, which is equivalent to the plug-in mechanism provided by struts1.x. The former is the filter provided by struts1.x, while interceptor is the filter provided by struts2.

Different from filter:

(1) do not configure in web.xml, but complete the configuration in struts.xml, together with action

(2) action can specify which interceptor to use to do things before receiving.

Differences and relationships between filters and interceptors in struts2:

(1) the interceptor is based on java reflection mechanism, while the filter is based on function callback.

(2) filters rely on servlet containers, while interceptors do not rely on servlet containers.

(3) interceptors can only work on Action requests, while filters can work 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 life cycle of Action, interceptors can be called multiple times, while filters can only be called once when the container is initialized.

This is the end of the content of "what is the difference and connection between servlet/filter/listener/interceptor". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report