Take Tomcat as an example to introduce the Servlet container startup process

Source: Internet
Author: User
Tags wrapper

Here, take Tomcat as an example to introduce the overall structure of the servlet container startup process Tomcat

Tomcat starts server servers to provide service services, container as the core component of service (container can connect multiple connector to form service), and services can be provided externally. Simply put, service is the marriage license of container and connector, and server servers provide services for the home.
Tomcat Container Model

From the above knowledge, Service provides Web application services externally, and the soul of service core component container is the servlet container. And the real management servlet is the context container.
The context container directly manages the wrapper class wrapper of the servlet in the container, and a Web application corresponds to a context container. Adding a Web application creates a context container and joins it into the parent container host.
How Web Engineering is started in the servlet container

1. The START process of the servlet container
Web applications are added to Tomcat by Tomcat instances, that is, Tomcat manages a newly added context container. As mentioned earlier, a Web application corresponds to a context container, which is the servlet container for the servlet runtime.
There is a Servletcontextlistener interface in the Servlet API that listens to the lifecycle of the ServletContext object, actually listening to the lifecycle of the Web application.
When a Web application starts or terminates, the Servletcontextevent event is triggered, which is handled by Servletcontextlistener. Two methods for handling Servletcontextevent events are defined in the Servletcontextlistener interface. Contextinitialized (Servletcontextevent event) and contextdestroyed (Servletcontextevent event) method, starting the web in the servlet container Called when applied and when the Web application is terminated. When the servlet container starts, it triggers the Servletcontextevent event and notifies the corresponding listener Servletcontextlistener; The servlet container monitors the state of the ServletContext (initialization or destruction of ServletContext) through Servletcontextlistener during the boot process; In Servletcontextlistener, the contextinitialized initialization method, According to the Web.xml to ServletContext configuration, the context container's properties are cached in memory for service services to use;

Contextconfig is added to ServletContext when Tomcat creates the context container, contextconfig the parsing of the configuration file (including Web.xml) that is responsible for the entire Web application.

2. Initialization of Web applications
The initialization of Web is implemented in Contextconfig, the initialization of the application is mainly to parse the Web.xml, this document describes the key information of Web application, and also the portal of the whole Web application.
When Tomcat resolves the web.xml file, it sets the properties in the context container, which includes creating a servlet instance, filter, and listener. Wraps the servlet into the wrapper class wrapper in the context.

3. Create a servlet instance
If the servlet's load-on-startup configuration in Web.xml is greater than 0, it is instantiated when the context container is started.

4. servlet initialization
By invoking the servlet's Init method, the Standarwrapperfacade that wraps the Standarwrapper object is passed to the servlet as a servletconfig.

The servlet is parsed from web.xml to initialization. The difference between ServletConfig and ServletContext ServletConfig: After a servlet is instantiated, any client access is valid at any time, but only for this servlet. A servlet's ServletConfig object cannot be accessed by another servlet.

5. The operating mechanism of the servlet
There are four object Servletcontext,servletconfig,servletrequest,servletresponse associated with the servlet.
The servlet's operating mode is a typical "handshaking interactive" run mode, ServletContext provides interactive scene patterns, and the initialization of the interactive scenario is described by ServletConfig. ServletRequest and Servletresponse are specific objects of interaction.

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.