The Tomcat startup process is explained in detail--very error: the loading process involving 2 files such as Web. xml

Source: Internet
Author: User
Tags server port

A detailed explanation of the Tomcat initiation process is presented in:Tomcat, Web Server, legacy archive | Author: A global person Label:Tomcat, principle, start-up process, detailed

Java-based Web applications are a collection of Servlets, JSP pages, static pages, classes, and other resources that can be packaged in a standard manner and run in multiple containers (such as Tomcat) from multiple vendors. A WEB application exists in the directory of a structured hierarchy that is defined by the Java Servlet specification. The root directory of a WEB application contains all the public resources, images, HTML pages, and so on, that are stored directly or stored in subfolders. Composition: A Web application consists of a Web component (a set of Java class Libraries), an HTML file, a static resource file (like), a helper class, and a library.

1–tomcat Server components (server.xml-file contents at the bottom of this document)

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 a. Share a single Engine
A service is a collection that consists of one or more connector, and an engine that handles all customer requests received by connector.

1.3–connector

A connector will listen for the customer request on a specified port and give the request to the engine to process, receive a response from the engine and return to the customer
Tomcat has two typical connector, one that listens directly to HTTP requests from browser, and one that listens for requests from other webserver
Coyote http/1.1 Connector listens for Http requests from customer browser at port 8080
Coyote JK2 Connector listens for 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 particular Service
It receives and processes all requests from one or more connectors
and returns the completed response to the Connector for ultimate transmission back to the client
The engine can be configured with multiple virtual hosts, each hosting a domain name
When the engine obtains a request, it matches the request to a host and then gives the request to the host to handle
The engine has a default virtual host that, when the request cannot be matched to any host, is given to the default host for processing

1.5–host

Represents a virtual host, which matches each virtual host with a network domain name
One or more web apps can be deployed under each virtual host, each Web app corresponds to a context with a context path
When host obtains a request, it matches the request to a context and then gives the request to the context to process
The matching method is "longest match", so a path== "" context will be the default context for that host
All requests that cannot match the path names of other context will eventually match that default context

1.6–context

A context corresponds to a Web application, and a Web application consists of one or more servlets
The context will be loaded into the Servlet class based on the profile $catalina_home/conf/web.xml and $webapp_home/web-inf/web.xml when it is created
When the context obtains the request, it looks for the matching servlet class in its own mapping table (mapping table)
If found, executes the class, obtains the requested response, and returns

2–tomcat server's structure diagram

Description of the 3– configuration file $catalina_home/conf/server.xml

This file describes how to start the Tomcat Server

<!----------------------------------------------------------------------------------------------->
<!--start server waits for the shutdown command at Port 8005 if you accept the "SHUTDOWN" string, shut down the server--
<server port= "8005" shutdown= "shutdown" debug= "0" >
<!--Listener??? Not currently seen here--
<listener classname= "Org.apache.catalina.mbeans.ServerLifecycleListener" debug= "0"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug= "0"/>
<!--Global JNDI Resources??? Not currently seen here, first omitted--
<GlobalNamingResources>
... ... ... ...
</GlobalNamingResources>
<!--Tomcat's standalone service service is a collection of connector they share an engine to handle all connector received requests--
<service name= "Tomcat-standalone" >
<!--Coyote http/1.1 Connector className: The Connector implementation class is Org.apache.coyote.tomcat4.CoyoteConnector port:
Listen for HTTP1.1 request minprocessors from customer browser at port number 8080: The connector first creates 5 threads waiting for a customer request.
One thread is responsible for each request Maxprocessors: When an existing thread is not enough to service a client request, if the total number of threads is less than 75, a new thread is created to process the request
Acceptcount: When the existing thread has reached the maximum of 75 o'clock, queued for customer requests when the number of requests in the queue exceeds 100, subsequent requests return connection refused
Error Redirectport: When the customer request is HTTPS, forward the request to port 8443 to go to the other properties---
<connector classname= "Org.apache.coyote.tomcat4.CoyoteConnector"
port= "8080"
minprocessors= "5" maxprocessors= "acceptcount=" 100 "
Enablelookups= "true"
Redirectport= "8443"
debug= "0"
connectiontimeout= "20000"
Useurivalidationhack= "false"
Disableuploadtimeout= "true"/>
<!--engine is used to process HTTP requests received by connector it will match the request and its own virtual host,
and transfer the request to the corresponding host to handle the default virtual host is localhost---
<engine name= "Standalone" defaulthost= "localhost" debug= "0" >
<!--log class, not currently seen, omitted first---
<logger classname= "Org.apache.catalina.logger.FileLogger" .../>
<!--Realm, not currently seen, omitted----
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm" .../>
<!--virtual host localhost appBase: The root directory of the virtual host is webapps/it will match the request and
The path of your own context and transfer the request to the corresponding context to handle--
<!--log class, not currently seen, omitted first---
<logger classname= "Org.apache.catalina.logger.FileLogger" .../>
<!--context, corresponding to a Web App path: The path name of the context is "", so the context is the host's
Default context DocBase: The context's root directory is webapps/mycontext/--
<context path= "" docbase= "mycontext" debug= "0"/>
<!--Another context, the path name is/wsota--
<context path= "/wsota" docbase= "Wsotaproject" debug= "0"/>
</Host>
</Engine>
</Service>
</Server>
<!----------------------------------------------------------------------------------------------->

Description of the 4–context deployment configuration file, Web. xml

A context corresponds to a web app, where each web app is made up of one or more servlets
When a web app is initialized, it will load each servlet class defined in the deployment configuration file Web. XML with its own ClassLoader object
It first loads into the servlet class deployed in $catalina_home/conf/web.xml
Then load the servlet classes deployed in Web-inf/web.xml in the root directory of your web App
The Web. xml file has two parts: the servlet class definition and the servlet mapping definition
Each loaded servlet class has a name and is filled into the context's mapping table (mapping table), corresponding to a URL pattern
When the context obtains the request, it queries the mapping table, finds the requested servlet, and executes to obtain the request response

Analyze the Web. xml file under Conf in all the context shared Tomcat root directories, where the servlet defined is loaded into

<!----------------------------------------------------------------------------------------------->
<web-app>
<!--Overview: This file is a common deployment profile for all Web apps, whenever a web app
Be deploy, the file will be processed first, then the WEB app's own/web-inf/web.xml--
<!--+-------------------------+-
<!--| servlet Class Definition Section | -
<!--+-------------------------+-
<!--Defaultservlet
The servlet is executed when the user's HTTP request does not match any one servlet.
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
Processing an anonymous servlet in a web app when a servlet is written and compiled into
/web-inf/classes/, but not when defined in/web-inf/web.xml
The servlet is called to map the anonymous servlet to the/servlet/classname form
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
This servlet is called when a JSP page is requested (*.JSP)
It is a JSP compiler that compiles the requested JSP page into a servlet and executes
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 Mapping Definition Section | -
<!--+---------------------------+-
<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 parts, omit first | -
<!--+------------------------+-
... ... ... ...
</web-app>
<!----------------------------------------------------------------------------------------------->

5–tomcat server process for processing an HTTP request

Suppose the request from the customer is:

http://localhost:8080/wsota/wsota_index.jsp

1) The request is sent to the native port 8080, where the Coyote http/1.1 connector that is listening is obtained
2) connector the request to the engine of the service it is working on and waits for a response from the engine
3) The engine obtains the request localhost/wsota/wsota_index.jsp, matches all the virtual host hosts it owns
4) The engine matches the host named localhost ( even if it does not match the request to the host processing, Because the host is defined as the engine's default host-that is, the match will not go back to find the engine in the name of localhost in the default host processing )
5) localhost host gets the request/wsota/wsota_index.jsp, matching all the context it owns
6) The host matches to the context where the path is/wsota ( if the request is not matched to the context of the path named "", the path in the context is "" The default processing context for that host)
7) path= "/wsota" The context gets the request/wsota_index.jsp, in its mapping table to find the corresponding servlet
8) Context matches to a servlet with a URL pattern of *.jsp, corresponding to the Jspservlet class
9) Constructs the HttpServletRequest object and the HttpServletResponse object, calling Jspservlet's Doget or Dopost method as a parameter
10) Context returns the HttpServletResponse object to host after it has finished executing
11) Host returns the HttpServletResponse object to the engine
12) Engine returns the HttpServletResponse object to connector
13) Connector return the HttpServletResponse object to the customer browser

Transferred from: http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html

Permanent link : http://www.ha97.com/4820.html

The default file Server.xml content under Tomcat's conf (some comments have been removed):

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<serverPort= "8005" shutdown= "Shutdown" >
<!--APR Library loader. Documentation At/docs/apr.html--
<listener classname= "Org.apache.catalina.core.AprLifecycleListener" sslengine= "on"/>
<!--Initialize Jasper prior to WebApps is loaded. Documentation At/docs/jasper-howto.html--
<listener classname= "Org.apache.catalina.core.JasperListener"/>
<!--Prevent memory leaks due to use of particular Java/javax apis-->
<listener classname= "Org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<!--JMX support for the TOMCAT server. Documentation At/docs/non-existent.html--
<listener classname= "Org.apache.catalina.mbeans.ServerLifecycleListener"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase"
description= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
</GlobalNamingResources>
<serviceName= "Catalina" >
<connector port= "8080" protocol= "http/1.1 "
connectiontimeout= "20000"
redirectport= "8443" uriencoding= "UTF-8"/>
<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
<engineName= "Catalina" defaulthost= "localhost" >
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"
Resourcename= "Userdatabase"/>
Name= "localhost" appbase= "WebApps"
Unpackwars= "true" autodeploy= "true"
Xmlvalidation= "false" Xmlnamespaceaware= "false" >
</Host>
</Engine>
</Service>
</Server>

The Tomcat startup process is explained in detail--very error: the loading process involving 2 files such as Web. xml

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.