In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
3. The processing flow of HTTP request in Web application
After traversing the Web container, the HTTP request will be delivered to the Web application, and we will continue to take Tomcat as an example to analyze the subsequent process. The connection between the Web container and the Web application is done through the configuration file web.xml. Web.xml is a configuration file that conforms to the Java Servlet standard specification. Through this configuration file, we define various core components and initialization configurations that make up the Web application, including filter Filter, listener Listener, server Servlet and so on. Different components undertake different functions. Before introducing the process of Web application processing HTTP requests, let's take a look at these core components as usual.
3.1 introduction to core components of Web application 3.1.1 filter Filter
The filter Filter is responsible for preprocessing the HTTP request, then handing the request to Servlet for processing and generating the response, and finally Filter performs post-processing on the response. From the perspective of the processing of HTTP requests, Filter is mainly involved in the following steps:
Intercept the customer's HttpServletRequest before HttpServletRequest arrives at Servlet. Check the HttpServletRequest as needed, or you can modify the HttpServletRequest header and data. Intercept the HttpServletResponse before the HttpServletResponse generated by Servlet reaches the client. Check the HttpServletResponse as needed, or you can modify the HttpServletResponse header and data.
Filter mapping filter-mapping, which is used to declare the filter that will be used by the Web application, which can be mapped to a Servlet or URL schema. If you map a filter to a Servlet, it acts on a specific Servlet. If you map a filter to an URL schema, it will act on any resource, as long as the URL of that resource matches the URL schema. If the request for a resource matches to more than one Filter, during the processing of the HTTP request, the Tomcat will be executed in the order in which the filter mapping filter-mapping is executed in the configuration file web.xml, first first and later later, and multiple filter Filter can form a call chain. There are three types of rules for URL pattern matching:
Exact match: for example, "/ foo.htm", that only matches the URL of "foo.htm". Path matching: for example, "/ foo/*", that only matches URL prefixed with foo. Suffix matching: for example, "* .htm", that only matches all URL with the suffix ".htm". EncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter / * 3.1.2 listener Listener
The listener Listener is mainly used to listen for changes in Application, Session, Request and other objects. Whenever these objects change, the corresponding listening methods will be called back. For example, there is a ServletContextListener interface in Servlet API, which can listen to the life cycle of ServletContext objects, which is actually listening to the life cycle of Web applications. When the Servlet container starts or terminates the Web application, the ServletContextEvent event is triggered, which is handled by ServletContextListener.
Org.springframework.web.util.Log4jConfigListener3.1.3 server Servlet
The server Servlet is responsible for handling HTTP requests from clients to access dynamic resources, and the interface javax.servlet.Servlet defines all the methods that must be implemented by Servlet.
Method name function description destroy () is called by the Servlet container to shut down the service provided by Servlet getServletConfig () to obtain the configuration information of the parameters at initialization and startup of Servlet. The object ServletConfiggetServletInfo () acquires the description information of Servlet, including: author, version, copyright, etc. Init () is called by the Servlet container, Servlet initialization is completed by the configuration ServletConfig, and the external service service () is called by the Servlet container, and the Servlet handles a HTTP request.
From the perspective of the processing of HTTP requests, the server Servlet is mainly involved in the following steps:
Receive request: the client request is encapsulated into a HttpServletRequest object, which contains information such as header parameters and body. Handling requests: usually call Servlet methods such as service, doPost or doGet to process the request, and further call the corresponding logic of the business layer to process it. Feedback response: after processing the request, you can forward (forward), redirect (redirect) to a view page, or return the result data directly. Forwarding is the method of HttpServletRequest, and redirection is the method of HttpServletResponse.
In the veteran's school years, the application of Web is relatively simple, mainly a variety of information management systems. At that time, Spring had not yet been born, and the mainstream technology stack was JSP/Servlet. When I developed Web applications, I mainly wrote and inherited from HttpServlet subclasses, and handed over various business logic functions to different HttpServlet subclasses. HttpServlet inherits from GenericServlet, which implements the interface Servlet. With the continuous development of digitization and Internet, the business system becomes more and more complex, the HttpServlet subclasses are written more and more, and the web.xml configuration files are more and more complex, which makes the expansion and maintenance of the system more and more difficult, and the manual workshop development method has been unable to keep up with the pace of business development. The hero of the times, Spring is called out in this context, it creatively invented control inversion IOC and aspect-oriented programming AOP, greatly reducing complexity. As shown in the following configuration example, only one Servlet needs to be configured for the entire Web application, which is the pre-dispenser DispatcherServlet of Spring:
Flow of mvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:mvc-servlet.xml 1 mvc / api3.2 Web applications processing HTTP requests
As shown in the following figure, the Web application processes HTTP requests through the listener Listener and the filter chain Filters, and finally reaches the server Servlet:
Previously, we exposed the complexity of Web applications directly to Tomcat, but now Spring has taken over the complexity of Web applications through new technologies such as control inversion IOC and aspect-oriented programming AOP. As in the previous configuration example of Servlet, the entire Web application only needs to configure the pre-dispenser DispatcherServlet provided by Spring. Developers no longer need to write and configure HttpServlet subclasses. All business logic functions will be developed in accordance with the standard specification of Spring, from writing Listener\ Filter\ Servlet to writing Spring Component, including Controller, Service, Repository and so on.
After Tomcat receives a Http request from a Web application, it will transfer all requests to the pre-distributor DispatcherServlet, and then DispatcherServlet will dispatch the request to specific business logic for processing. DispatcherServlet is a subclass derived from HttpServlet, and its class diagram relationship is as follows:
3.3 Analysis of the evolution process of Web application architecture
The evolution of Web application architecture is like a start-up incubation process. At first, the product created by the startup team is very simple, and the manual workshop model is used to quickly create the minimum feasible product. At this time, the organizational structure of the team is as flat and simple as the product. However, as the product is used by more and more users, the function becomes more and more complex, and then a new architecture must be introduced to effectively manage complexity. at the same time, the expansion of the team size also needs an organizational structure that matches the business development. only in this way can we ensure the continuous development of the product. The evolution of Web application architecture is similar to the formation of Tomcat architecture. Veterans often use the "Russian doll" model to explain the architecture. The architecture principles of Web container and Web application are similar, just like big dolls with small dolls.
Spring's IOC container is similar to Tomcat's Servlet container, which defines components through configuration files, and then initializes and adds these defined components to the container during startup, and then finds them from the container for subsequent use. Early Web applications are mainly composed of components such as Filter\ Listener\ Servlet written by a large number of developers, and the configurations of these core components are all maintained through web.xml configuration files, so there is no loose coupling between Web applications and Web containers, but after the introduction of Spring, only a small number of components such as DispatcherServlet need to be configured, which meets the requirements of hierarchical architecture and is more conducive to the development of complex Web applications. If we use the technical terms of architecture to describe it, this is the classic hierarchical architecture model, which is loosely coupled between layers, only through a small number of interfaces, and each layer is highly cohesive.
The main value of this article is to help you sort out the end-to-end framework of the whole process, that is, the global perspective or God perspective. With this framework, we can according to our own needs to find clues to find the relevant node information to study, so as not to fall into the details can not find the direction. Of course, considering that our work and study conditions are different and the problems we usually encounter are also different, this article can not cover the problems encountered by everyone. You are welcome to leave messages and questions, and you are welcome to follow my blog or the official name "IT veteran brother" to communicate and interact. I will try my best to answer your questions as soon as possible. Thank you!
The index of other articles in this series is as follows:
Diagram Spring:HTTP request processing flow and mechanism [1] Diagram Spring:HTTP request processing flow and mechanism [2] Diagram Spring:HTTP request processing flow and mechanism [4] Diagram Spring:HTTP request processing flow and mechanism [5]
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.