The internal principle of Tomcat

Source: Internet
Author: User
Tags class definition tomcat server

1 Components of 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 components so share a single Engine
A service is a collection of one or more connector, as well as a engine, responsible for handling client requests obtained by all connector

1.3-connector

A connector will listen on a client request on a specified port and hand over the received request to engine, receive a response from 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 on port 8009 for SERVLET/JSP agent requests from other webserver (Apache)

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
Engine can be configured with more than one virtual host, each virtual host has a domain name
When engine obtains a request, it matches the request to a host and then gives the request to the host to handle
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 and a network domain name Domain name
One or more web apps can be deployed under each virtual host (deploy), each web app corresponds to a context and has a path
When the host obtains a request, it will match the request to a context, and then give the request to the context to process
The matching method is "longest match", so a path== "" context becomes the default context for that host
All requests that cannot match the path names of the other context will eventually match the default context

1.6-context

A context corresponds to a Web application, a Web application consisting of one or more servlet
The context will load the servlet class according to the configuration file $catalina_home/conf/web.xml and $webapp_home/web-inf/web.xml when it is created
When the context gets a request, it looks for a 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


3-Description of configuration file $catalina_home/conf/server.xml
This file describes how to start Tomcat 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 servlet
When a web app is initialized, it loads each servlet class defined in deployment profile web.xml with its own ClassLoader object
It first loads the servlet classes deployed in $catalina_home/conf/web.xml
Then load the servlet classes deployed in the Web-inf/web.xml in your own web App root directory
The Web.xml file has two parts: the servlet class definition and the servlet mapping definition
Each loaded servlet class has a name that is filled in the context's mapping table (mapping table) and corresponds to a URL pattern
When the context gets the request, it queries the mapping table, finds the requested servlet, and executes to obtain a request response

Analyze all the context-shared web.xml files in which the servlet defined is loaded by all web apps

The process of 5-tomcat Server processing an HTTP request
Suppose the request from the customer is:
The 1 http://localhost:8080/wsota/wsota_index.jsp request was sent to native port 8080, where Coyote http/1.1, which was listening, obtained

2 connector the request to the engine of its service and waits for a response from engine

3 engine Obtain the request localhost/wsota/wsota_index.jsp, match all the virtual host host it owns

4 engine is matched to the host named localhost (even if the match is not matched, the request is given to the host, because the host is defined as the default host for the engine)

5 localhost host obtains the request/wsota/wsota_index.jsp, matches all the context it owns

6 host matches to the context where the path is/wsota (if the match is not matched, give the request to the context of the path named "")

7 Path= "/wsota" context Get request/wsota_index.jsp, find the corresponding servlet in its mapping table

8 context matching servlet to URL pattern *.jsp, corresponding to the Jspservlet class

9 Constructing HttpServletRequest objects and HttpServletResponse objects, calling Jspservlet Doget or Dopost methods as parameters

The context returns the HttpServletResponse object after execution to the host

One host returns the HttpServletResponse object to engine

Engine the HttpServletResponse object back to Connector

Connector returns the HttpServletResponse object to the customer browser

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.