A: the preface

The web. XML file is normally located in the WEB-INF directory.
If you’ve read the Tomcat source code, you know that during the initialization phase of Tomcat’s startup process, the web.xml configuration is read.

Example 2:

Spring parent container configuration


SpringMvc child container configuration


The above content is configured in web.xml, divided into spring parent container and springMVC child container configuration.
The ApplicationContext of the child container is associated with the ApplicationContext of the parent container.

3.1 ContextLoaderListener3.2 DispatcherServlet

(1) Introduction
The inheritance relationship of DispatcherServlet is as follows:







In Spring, ContextLoaderListener is just a helper function for creating instances of WebApplicationContext. The actual logic is implemented in the DispatcherServlet. DispatcherServlet is an implementation class that implements the Servlet interface.
Servlet is a Program written in Java, which is based on HTTP protocol and runs on the server side (such as Tomcat). It is a Java class written according to the servlet specification. The main task is to process the client’s request and send the result to the client. The servlet life cycle is controlled by the servlet container and can be divided into three phases: initialization, running, and destruction.
(1) Initialization stage
  • The servlet container loads the servlet class and reads the data from the servlet class’s.class file into memory.
  • The servlet container creates a ServletConfig object. The ServletConfig object contains the initial configuration information for the servlet.
  • The servlet container creates a servlet object.
  • The servlet container calls the init method of the Servlet object to initialize it.
(2) Operation stage.
When the Servlet container receives a request, the Servlet container creates servletRequest and servletResponse objects for the request, and then calls the Service method. And pass these two parameters to the service method. The Service method gets the requested information from the servletRequest object. Process the request. The response to this request is generated using the servletResponse object. The servletRequest and servletResponse objects are then destroyed. Regardless of whether the request is submitted by POST or by GET, the service method will eventually handle the request.
(3) Destruction stage.
When a Web application is terminated, the Servlet container first calls the deStory method on the servlet object and then destroys the servlet object, along with the servletConfig object associated with the servlet object. We can implement the deStory method to release the resources occupied by the servlet, such as closing the database connection, closing the file input and output stream, etc.
The servlet framework consists of two Java packages: Javax. servlet and javax.servlet.http. The javax.servlet package defines the generic interfaces and classes that all servlet classes must implement or extend, and the HttpServlet class using the HTTP communication protocol is defined in the Javax.servlet. HTTP package.
Servlet is designed to be request-driven. A servlet request may contain multiple data items. When a Web container receives a servlet request, the servlet encapsulates the request into an HttpServletRequest object, which is then passed to the corresponding service method of the servlet.
(2) Initialization of DispatcherServlet
Its init method is called during the servlet initialization phase, so we first need to see if init method is overridden in DispatcherServlet. We found the method init() in its parent class HttpServletBean
The related methods are called step by step during initialization:
The init () -”
InitServletBean () -”
InitWebApplicationContext () -”
CreateWebApplicationContext (rootContext) -”
CreateWebApplicationContext (ApplicationContext parent) -” configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac)
Among them, added a ApplicationListener configureAndRefreshWebApplicationContext method, It initializes the related components in the initStrategies(ApplicationContext Context) method of onRefresh(ApplicationContext Context) :
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
The purpose of the ContextLoaderListener is to automatically assemble the configuration information of the ApplicationContext when the Web container is started.
Because it implements the ServletContextListener interfaceUsing the ServletContextListener interface, developers can add arbitrary objects to the ServletContext before servicing client requests. This object is initialized when the ServletContext is started, and is visible throughout the ServletContext’s run.
Each Web application has a ServletContext associated with it. The ServletContext object is created when the application is started and destroyed when the application is closed. A ServletContext is valid globally, similar to a global variable in an application.
The core logic in the ServletContextListener is to initialize the WebApplicationContext instance and store it in the ServletContext.
The ServletContextListener interface is as follows. During the startup of containers such as Tomcat, the contextInitialized method is called to load the ApplicationContext container
public interface ServletContextListener extends EventListener {
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
}

For more technical information: Gzitcast