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 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.
Components of the 1–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 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. xmlA 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 all the context-shared web. xml files in which the servlet defined is loaded into all
<!----------------------------------------------------------------------------------------------->
<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 requestSuppose 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)
5) localhost host gets the request/wsota/wsota_index.jsp, matching all the context it owns
6) host matches to the context where the path is/wsota (if the match is not matched, the request is given to the context of the path named "" to process)
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
) context returns the HttpServletResponse object to host after it has finished executing
) host returns the HttpServletResponse object to the engine
) engine returns the HttpServletResponse object to connector
Connector return the HttpServletResponse object to the customer browser
Reprint: Tomcat Process principle