Tomcat startup Analysis

Source: Internet
Author: User
Document directory
  •  
  • 1-components of Tomcat server
  • 2-Tomcat server structure
  • 3-Description of the configuration file $ catalina_home/CONF/server. xml
  • 4-Description of the deployment configuration file web. xml of Context
  • 5-Tomcat server processes an HTTP request
(Conversion) Tomcat startup big Analysis 1-components of Tomcat server 1.1-Server

A server element represents the entire Catalina servlet container. (Singleton)

1.2-Service

A service element represents the combination of one or more connector components that share a single engine
A service is a collection composed of one or more ctor s and an engine that processes customer requests obtained by all connector S.

1.3-connector

A connector listens to customer requests on a specified port, sends the obtained requests to the engine for processing, obtains responses from the engine, and returns the customer
Tomcat has two typical ctor, one directly listening for HTTP requests from browser and the other listening for requests from other webservers.
Coyote HTTP/1.1 Connector listens for HTTP requests from the client browser at port 8080
Coyote JK2 connector listens to Servlet/jsp proxy requests from other webserver (APACHE) at Port 8009

1.4-engine

The engine element represents the entire request processing machinery associated with a participant Service
It has es and processes all requests from one or more connectors
And returns the completed response to the connector for ultimate transmission back to the client
Multiple virtual hosts can be configured in the engine. Each virtual host has a domain name.
When the engine obtains a request, it matches the request to a host, and then delivers the request to the host for processing.
The engine has a default virtual host. When the request cannot match any host, the default host is used for processing.

1.5-host

Represents a virtual host. Each virtual host matches the domain name of a network domain.
Deploy one or more web apps under each virtual host. Each web app corresponds to a context and has a context path.
When the host receives a request, it matches the request to a context, and then sends the request to the context for processing.
The matching method is "Longest match", so a context with Path = "" will become the default context of the host.
All requests that cannot match other context path names will eventually match the default context.

1.6-Context

A context corresponds to a web application. A Web application consists of one or more servlets.
Context will load the servlet class according to the configuration file $ catalina_home/CONF/Web. xml and $ webapp_home/WEB-INF/Web. xml at creation
When the context obtains the request, it searches for the matching servlet class in its own mapping table.
If yes, this class is executed to obtain the request response and return

2-Tomcat server structure

3-Description of the configuration file $ catalina_home/CONF/server. xml

This file describes how to start Tomcat server
<! Latency>

<Web-app>


<! -- Overview:
This file is the deployment configuration file shared by all web apps,
Every time a web app is deploy, the file will be processed first, and then the web app's own/WEB-INF/Web. xml
-->

<! -- + ------------------------- + -->
<! -- | Servlet class definition | -->
<! -- + ------------------------- + -->

 

<! -- Defaservlet Servlet
When the user's HTTP request cannot match any servlet, the servlet is executed
URL pattern mapping :/
-->

<Servlet>
<Servlet-Name> default </servlet-Name>
<Servlet-class>
Org. Apache. Catalina. servlets. DefaultServlet
</Servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 0 </param-value>
</Init-param>
<Init-param>
<Param-Name> listings </param-Name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>


<! -- Invokerservlet
Process anonymous Servlet in a web app
When a servlet is written and compiled into/WEB-INF/classes/, but not defined in/WEB-INF/Web. xml
This servlet is called to map an anonymous Servlet into the form of/servlet/classname
URL pattern mapping:/servlet /*
-->

<Servlet>
<Servlet-Name> invoker </servlet-Name>
<Servlet-class>
Org. Apache. Catalina. servlets. invokerservlet
</Servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 0 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>


<! -- Jspservlet
When a JSP page is requested (*. jsp), the servlet is called.
It is a JSP compiler that compiles the requested JSP page into a Servlet and then executes it.
URL pattern mapping: *. jsp
-->

<Servlet>
<Servlet-Name> JSP </servlet-Name>
<Servlet-class> org. Apache. Jasper. servlet. jspservlet </servlet-class>
<Init-param>
<Param-Name> logverbositylevel </param-Name>
<Param-value> warning </param-value>
</Init-param>
<Load-on-startup> 3 </load-on-startup>
</Servlet>


<! -- + --------------------------- + -->
<! -- | Servlet ing definition | -->
<! -- + --------------------------- + -->


<Servlet-mapping>
<Servlet-Name> default </servlet-Name>
<URL-pattern>/</url-pattern>
</Servlet-mapping>

<Servlet-mapping>
<Servlet-Name> invoker </servlet-Name>
<URL-pattern>/servlet/* </url-pattern>
</Servlet-mapping>

<Servlet-mapping>
<Servlet-Name> JSP </servlet-Name>
<URL-pattern> *. jsp </url-pattern>
</Servlet-mapping>


<! -- + ------------------------ + -->
<! -- | Other part, skip first | -->
<! -- + ------------------------ + -->

............

</Web-app>

<! Latency>

<! Latency>


<! -- Start server
Wait for the command to close at Port 8005
If the "shutdown" string is received, the server is disabled.
-->

<Server port = "8005" shutdown = "shutdown" DEBUG = "0">


<! -- Listener ???
Not found here currently
-->

<Listener classname = "org. Apache. Catalina. mbeans. serverlifecyclelistener" DEBUG = "0"/>
<Listener classname = "org. Apache. Catalina. mbeans. globalresourceslifecyclelistener" DEBUG = "0"/>


<! -- Global JNDI resources ???
I can't see it now. Skip it first.
-->

<Globalnamingresources>
............
</Globalnamingresources>


<! -- Standalone service of Tomcat
A service is a collection of connectors.
They share an engine to process all requests received by the ctor.
-->

<Service name = "tomcat-standalone">


<! -- Coyote HTTP/1.1 Connector
Classname: The org ctor implementation class is org. Apache. Coyote. tomcat4.coyoteconne.
Port: listening for http1.1 requests from client browser at port 8080
Minprocessors: this connector ctor creates five threads to wait for customer requests. One thread is responsible for each request.
Maxprocessors: when the existing threads are insufficient to serve customer requests, if the total number of threads is less than 75, a new thread is created to process requests.
Acceptcount: when the number of existing threads reaches 75, the request is queued for the customer.
When the number of requests in the queue exceeds 100, the subsequent requests return the connection refused error.
Redirectport: when the customer requests https, the request is forwarded to port 8443.
Other attributes
-->

<Connector classname = "org. Apache. Coyote. tomcat4.coyoteconnector"
Port = "8080"
Minprocessors = "5" maxprocessors = "75" acceptcount = "100"
Enablelookups = "true"
Redirectport = "8443"
DEBUG = "0"
Connectiontimeout = "20000"
Useurivalidationhack = "false"
Disableuploadtimeout = "true"/>


<! -- Engine is used to process HTTP requests received by ctor
It matches the request with its own virtual host and forwards the request to the corresponding host for processing.
The default virtual host is localhost.
-->

<Engine name = "standalone" defaulthost = "localhost" DEBUG = "0">


<! -- Log class, not seen at present, skip first -->

<Logger classname = "org. Apache. Catalina. Logger. filelogger".../>

<! -- Realm, not seen at present, skip first -->

<Realm classname = "org. Apache. Catalina. realm. userdatabaserealm".../>


<! -- Virtual host localhost
Appbase: the root directory of the VM is webapps/
It matches the path of the request and its own context, and transfers the request to the corresponding context for processing.
-->

<Host name = "localhost" DEBUG = "0" appbase = "webapps" unpackwars = "true" autodeploy = "true">


<! -- Log class, not seen at present, skip first -->

<Logger classname = "org. Apache. Catalina. Logger. filelogger".../>


<! -- Context corresponds to a web app
Path: The Path Name of the context is "", so the context is the default context of the host.
Docbase: the root directory of the context is webapps/mycontext/
-->

<Context Path = "" docbase = "mycontext" DEBUG = "0"/>


<! -- Another context, with the path name/wsota -->

<Context Path = "/wsota" docbase = "wsotaproject" DEBUG = "0"/>


</Host>

</Engine>

</Service>

</Server>

<! Latency>

4-Description of the deployment configuration file web. xml of Context

A context corresponds to a web app. Each web app is composed of one or more servlets.
When a web app is initialized, it loads each servlet class defined in "deployment configuration file web. xml" with its own classloader object.
It first loads the servlet class deployed in $ catalina_home/CONF/Web. xml.
Then load the servlet class deployed in the WEB-INF/Web. xml under the root directory of your web app
The Web. xml file has two parts: servlet class definition and Servlet ing definition.
Each loaded servlet class has a name and is filled in with the mapping table of the context, which corresponds to a URL pattern.
When the context obtains the request, it queries the mapping table, finds the requested servlet, and executes the request to obtain the request response.

Analyze all the web. xml files shared by context, and the servlet defined in it is loaded by all web apps.

 

5-Tomcat server processes an HTTP request

Assume that the request from the customer is:
HTTP: /localhost: 8080/wsota/wsota_index.jsp

1) The request is sent to the local port 8080, which is obtained by coyote HTTP/1.1 Connector
2) connector submits the request to the engine of the Service to process it, and waits for a response from the engine.
3) The engine obtains the request localhost/wsota/wsota_index.jsp, matching all the virtual host hosts it owns.
4) The engine matches the host named localhost (the request is handed over to the host even if the match fails, because the host is defined as the default host of the engine)
5) the localhost obtains the request/wsota/wsota_index.jsp and matches all the context
6) The host matches the context in the/wsota path. (if no match is found, submit the request to the context with the path name "" for processing)
7) obtain the request/wsota_index.jsp from the context of Path = "/wsota" and find the corresponding servlet in its mapping table.
8) the context matches the servlet whose URL pattern is *. jsp, which corresponds to the jspservlet class.
9) construct the httpservletrequest object and the httpservletresponse object, and call the jspservlet doget or dopost method as parameters.
10) Context returns the httpservletresponse object after execution to the host
11) The host returns the httpservletresponse object to the engine.
12) the engine returns the httpservletresponse object to Connector
13) connector returns the httpservletresponse object to the client browser.

From http://blog.csdn.net/xiong2009/archive/2006/10/21/1344071.aspx

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.