Tomcat startup 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
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
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 to process a default VM in the engine. When the request cannot match any
When a host is on, it will be handed over to the default host 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-configuration file $ catali
Description of na_home/CONF/server. xml
1 <! -- Start the server and wait for the command to close at Port 8005. If the "shutdown" string is received, close the server. -->
2
3 <server port = "8005" shutdown = "shutdown" DEBUG = "0">
4
5
6
7 <listener classname = "org. Apache. Catalina. mbeans. serverlifecyclelistener" DEBUG = "0"/>
8
9 <listener classname = "org. Apache. Catalina. mbeans. globalresourceslifecyclelistener" DEBUG = "0"/>
10
11
12
13
15 <globalnamingresources>
16
17
18
19 </globalnamingresources>
20
21
22
23 <! -- Standalone service of Tomcat
24
25 service is a collection of ctor S. They share an engine to process requests received by all connector ctor -->
26
27 <service name = "tomcat-standalone">
28
29 <! -- Coyote HTTP/1.1 Connector
30
31 classname: The org ctor implementation class is org. Apache. Coyote. tomcat4.coyoteconne.
32
33 port: listening for http1.1 requests from client browser at port 8080
34
35 minprocessors: this connector ctor creates five threads to wait for the customer's request. Each request is handled by one thread.
36
37. 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.
38
39 acceptcount: when the number of existing threads reaches 75, the request is queued for the customer.
40
41 when the number of requests in the queue exceeds 100, a connection refused error will be returned for subsequent requests.
42
43 redirectport: when the customer requests https, the request is forwarded to port 8443.
44
45 other attributes -->
46
47 <connector classname = "org. Apache. Coyote. tomcat4.coyoteconnector"
48
49 Port = "8080" minprocessors = "5" maxprocessors = "75" acceptcount = "100"
50
51 enablelookups = "true" redirectport = "8443" DEBUG = "0" connectiontimeout = "20000"
52
53 useurivalidationhack = "false" disableuploadtimeout = "true"/>
54
55 <! -- Engine is used to process HTTP requests received by ctor
56
57. It matches the request with its own virtual host, and forwards the request to the corresponding host to process the default virtual host is localhost -->
58
59 <engine name = "standalone" defaulthost = "localhost" DEBUG = "0">
60
61
63 <logger classname = "org. Apache. Catalina. Logger. filelogger"/>
64
66
67 <realm classname = "org. Apache. Catalina. realm. userdatabaserealm"/>
68
69 <! -- Virtual host localhost
70
71 appbase: the root directory of the VM is webapps/
72
73. It matches the path of the request and its own context and transfers the request to the corresponding context for processing.
74
75 -->
76
77
, Skip first -->
78
79 <logger classname = "org. Apache. Catalina. Logger. filelogger"/>
80
81 <! -- Context corresponds to a web app
82
83 path: the path name of the context is "", so the context is the default context of the host.
84
85 docbase: the root directory of the context is webapps/mycontext/-->
86
87 <context Path = "" docbase = "mycontext" DEBUG = "0"/>
88
89 <! -- Another context, with the path name/wsota -->
90
91 <context Path = "/wsota" docbase = "wsotaproject" DEBUG = "0"/> 92 </engine>
93 </service>
94 </Server>
95
96
This file describes how to start Tomcat server
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 uses its classloader
Load each servlet class defined in "deployment configuration file web. xml". It is first loaded into the servlet class deployed in $ catalina_home/CONF/Web. xml and then loaded into your web
The servlet web. xml file deployed in the WEB-INF/Web. xml under the app root directory 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. When a URL pattern pair needs the context to obtain the request
Query mapping table, find the requested servlet, and execute it 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.
1 <web-app>
2
3 <! -- Overview:
4
5. This file is a deployment configuration file shared by all web apps,
6
7 every time a web app is deploy, the file will be processed first, then the web app's own/WEB-INF/Web. xml
8
9 -->
10
11 <! -- | Servlet class definition | -->
12
13 <! -- Defaservlet Servlet
14
15 when the user's HTTP request cannot match any servlet, the servlet is executed
16
17 URL pattern mapping:/-->
18
19 <servlet>
20
21 <servlet-Name> default </servlet-Name>
22
23 <servlet-class>
24
25 org. Apache. Catalina. servlets. DefaultServlet
26
27 </servlet-class>
28
29 <init-param>
30
31 <param-Name> debug </param-Name>
32
33 <param-value> 0 </param-value>
34
35 </init-param>
36
37 <init-param>
38
39 <param-Name> listings </param-Name>
40
41 <param-value> true </param-value>
42
43 </init-param>
44
45 <load-on-startup> 1 </load-on-startup>
46
47 </servlet>
48
49
50
51 <! -- Invokerservlet
52
53 process anonymous Servlet in a web app
54
55 when a servlet is written and compiled into/WEB-INF/classes/, but not defined in/WEB-INF/Web. xml
56
57. The servlet is called and the anonymous Servlet is mapped to/servlet/classname.
58
59 URL pattern mapping:/servlet /*
60
61 -->
62
63 <servlet>
64
65 <servlet-Name> invoker </servlet-Name>
66
67 <servlet-class>
68
69 org. Apache. Catalina. servlets. invokerservlet
70
71 </servlet-class>
72
73 <init-param>
74
75 <param-Name> debug </param-Name>
76
77 <param-value> 0 </param-value>
78
79 </init-param>
80
81 <load-on-startup> 2 </load-on-startup>
82
83 </servlet>
84
85
86
87
88
89 <! -- Jspservlet
90
91 when a JSP page is requested (*. jsp), the servlet is called
92
93. It is a JSP compiler that compiles the requested JSP page into a Servlet and then executes it.
94
95 URL pattern mapping: *. jsp -->
96
97 <servlet>
98
99 <servlet-Name> JSP </servlet-Name>
100
101 <servlet-class> org. Apache. Jasper. servlet. jspservlet </servlet-class>
102
103 <init-param>
104
105 <param-Name> logverbositylevel </param-Name>
106
107 <param-value> warning </param-value>
108
</Init-param> 109
110
111 <load-on-startup> 3 </load-on-startup>
112
113 </servlet>
114
115 <! -- | Servlet ing definition | -->
116
117 <servlet-mapping>
118
119 <servlet-Name> default </servlet-Name>
120
121 <URL-pattern>/</url-pattern>
122
123 </servlet-mapping>
124
125 <servlet-mapping>
126
127 <servlet-Name> invoker </servlet-Name>
128
129 <URL-pattern>/servlet/* </url-pattern>
130
131 </servlet-mapping>
132
133
134
135 <servlet-mapping>
136
137 <servlet-Name> JSP </servlet-Name>
138
139 <URL-pattern> *. jsp </url-pattern>
140
141 </servlet-mapping>
142
143 <! -- | Other part, skip first | -->
144
145
146
147 </Web-app>
148
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.