Revisit the Web server--about Tomcat server

Source: Internet
Author: User

Beginning with the development of Java Web from the university began to use the Tomcat Deployment Web project, the understanding of it only stay in the "This is an open source free servlet container" phase, but also contact with some of the Tomcat system, principles and other aspects of knowledge, is semi-known, Recently began to see this aspect of things, as of writing this blog, I do not fully understand it, but some of the more basic things are finally some clues, read the source is not easy, and the line and cherish, here write a note to organize.

Tomcat architecture

Steal a picture first:

    

As you can see, Tomcat has a top-level container, that is, the server container, which is the largest, in the server container, can have multiple services, serviced by service, so the service must not have, at least one.

In the service, mainly contains two components, connector and Container.container is not marked, in fact, is the part that contains the engine.

Tomcat can provide a variety of protocol requests, HTTP protocol is one of them, here in HTTP request for example, an HTTP request sent to the server by connector to receive and convert, connector is used to handle the connection related work of the component, For example, you can switch between socket and request,response, container is a library containing a lot of resources such as servlet, it accepts requests from Connector, resolves the requested resources, Returned to container, so connector and container a master outside the Lord, two people together to establish a happy family.

Of course, the above analogy is not rigorous, because there is only one container in a service, but can have multiple connector, the following code from the Tomcat directory under Conf server.xml:

<Servicename= "Catalina">     <Connector  port= "8080"  protocol= "http/1.1"  ConnectionTimeout= "20000"  redirectport= "8443" /><Connector  port= "8009"  protocol= "ajp/1.3"  Redirectport= "8443" />    </Service>

As you can see, the default Tomcat service profile has two connector, one for listening on 8080 ports and one for listening on 8009 ports, which we are familiar with to listen to HTTP requests from the browser and then new to a thread to pass the request to the engine , while the latter is used to listen to other types of servlet/jsp requests, called the AJP protocol (which I have not heard).

Container architecture

Continue to steal diagrams, container architecture:

Container is the interface of the container in Tomcat, and the servlet we are most familiar with is encapsulated in the container sub-interface wrapper. Look at the configuration in Server.xml:

<Servicename= "Catalina">    <Enginename= "Catalina"Defaulthost= "localhost">      <RealmClassName= "Org.apache.catalina.realm.LockOutRealm">        <RealmClassName= "Org.apache.catalina.realm.UserDatabaseRealm"resourcename= "Userdatabase"/>      </Realm>      <Hostname= "localhost"AppBase= "WebApps"Unpackwars= "true"Autodeploy= "true">        <ValveClassName= "Org.apache.catalina.valves.AccessLogValve"Directory= "Logs"prefix= "Localhost_access_log"suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot;%s%b" />      </Host>    </Engine>  </Service>

Container has four sub-containers, according to the diagram and the XML configuration, it can be seen that they are layers of the relationship.

Only one engine,engine in each service can have multiple hosts, each host can have multiple context, each context can have more than one wrapper, and each of the wrapper contains a servlet.

Several containers are briefly summarized below:

Engine: As the name implies, it is used to manage multiple sites, that is, host.

Host: Represents a site, also known as a virtual host, in the XML configuration above, you can see that Tomcat is configured by default a virtual host named localhost, we deploy, run the project, the default access to this site, Tomcat goes back to the WebApps directory to locate the requested Web project resource.

Context: Meaning is a contextual, it represents an application, that is, we develop a Web project, a Web project can be understood as a context.

Wrapper: Each Wrapper encapsulates a servlet.

Each directory under WebApps in the default configuration is an app with one root directory representing the main application, and the entire WebApps represents a site (Host), which is typically turned on when we detect if Tomcat is successful http://localhost:8080 /, this time if the official Tomcat site will indicate the success of the launch, in fact, the access is the root application of resources, the main application is directly using the domain name access can be, assuming webapps there is a Helloword directory, If you want to access resources under the Helloword directory, you will need to enter http://localhost:8080/helloword/.

Configuration file Server.xml in the Conf directory
<ServerPort= "8005"shutdown= "SHUTDOWN">  <Servicename= "Catalina">    <ConnectorPort= "8080"Protocol= "http/1.1"ConnectionTimeout= "20000"Redirectport= "8443" />    <ConnectorPort= "8009"Protocol= "ajp/1.3"Redirectport= "8443" />    <Enginename= "Catalina"Defaulthost= "localhost">      <RealmClassName= "Org.apache.catalina.realm.LockOutRealm">        <RealmClassName= "Org.apache.catalina.realm.UserDatabaseRealm"resourcename= "Userdatabase"/>      </Realm>      <Hostname= "localhost"AppBase= "WebApps"Unpackwars= "true"Autodeploy= "true">        <ValveClassName= "Org.apache.catalina.valves.AccessLogValve"Directory= "Logs"prefix= "Localhost_access_log"suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot;%s%b" />      </Host>    </Engine>  </Service></Server>

In Server.xml, a server was first configured to listen for the shutdown command "shutdown" on port 8005.

The server defines a service,service named Catania that defines two connector, one for the HTTP protocol, one for the AJP protocol, and one for the engine named Catalina. A host named localhost is defined in the engine.

The name attribute in host indicates the domain name, so this default host can be accessed with localhost, the AppBase property specifies the location of the site, here is the WebApps directory, there are many properties, detailed introduction, you can refer to this blog:/http Www.blogjava.net/baoyaer/articles/107278.html

Container architecture

Do not steal the picture, draw a picture yourself:

Connector is the socket connection used to receive requests and encapsulate requests into request and response for specific business processing.

Connector implementation of the TCP/IP protocol and HTTP protocol, he will be the request and response in accordance with the HTTP protocol to the encapsulation, the package is finished and handed over to container for processing, after the container processing, and then return back, Connector uses the socket to return the returned results to the browser to complete the processing request.

Here is a brief introduction to several important components in connector

Protocolhandler: Handles requests for different connection types, such as regular socket requests and niosocket requests.

Endpoint: Handles the network connection of the underlying socket. The TCP/IP protocol is implemented.

Processor: Encapsulates the socket request received by endpoint into the HTTP protocol that is requested, implemented.

Adapter: The request is assigned to the container for processing.

  

The entire Tomcat server is actually a Java writing application, I try to read some source code, but the qualifications are shallow, read very difficult, some code implementation and principles do not understand, can only understand some of the functions of the class, so here is just a brief summary of some relatively superficial knowledge, later, Have the determination and perseverance to study again. Mutual encouragement.

Resources:

< see spring MVC source > seventh: Tomcat analysis

Revisit the Web server--about Tomcat server

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.