A brief talk on the implementation principle of Tomcat

1, Foreword

After we came into contact with java, I believe everyone has written server programs. Tomcat is needed at this time. Tomcat server is an open source lightweight Web application server, which is widely used in small and medium-sized systems and occasions with small concurrency. It is the first choice for developing and debugging Servlet and JSP programs.

2, Basic principles of Tomcat

  1. Tomcat architecture

Main components of Tomcat: server, Service, connector, Container. Connector and Container are the core of Tomcat. Tomcat also has other important components, such as security, logger, log, session, mbeans, naming and other components. Together, these components provide the necessary services for connector and Container. A Container container and one or more connectors, together with other supported components, form a Service service. With Service services, you can provide external capabilities. However, the survival of Service services requires an environment, which is server. Server components provide a living environment for the normal use of Service services, The server component can manage one or more Service services at the same time.

  • Main parts:
  • Server: refers to the entire Tomcat server, including multiple groups of services. It is responsible for managing and starting various services, and listening to the shutdown command sent from port 8005 to close the entire container;
  • Service: Tomcat encapsulates and provides a complete component-based web service externally, including two core components: Connectors and Container, and multiple functional components. Each service is independent, but shares the resources of the same JVM;
  • Connector: the connector between Tomcat and the outside world. It listens to the fixed port, receives external requests, passes them to the Container, and returns the Container processing results to the outside world;
  • Container: Catalina, a Servlet container. It is composed of multiple containers. It is used to manage the Servlet life cycle and call Servlet related methods.
  • Loader: encapsulates Java ClassLoader, which is used to load class files in Container;
  • Realm: Tomcat provides access authentication and role management mechanism for web applications;
  • JMX: the technical specification defined in Java SE is a framework for implanting management functions for applications, devices and systems. The running state of Tomcat can be monitored remotely through JMX;
  • Jasper: Tomcat's Jsp parsing engine, which is used to convert Jsp into Java files and compile them into class files. Session: it is responsible for managing and creating sessions, as well as the persistence of sessions (customizable), and supports the set of sessions
  • Pipeline: acts as a pipeline in the container. Various valves can be set in the pipeline. Requests and responses are processed through various valves in the pipeline, providing a flexible and configurable mechanism for processing requests and responses.
  • Naming: naming service, JNDI, Java Naming and directory interface, is a set of API s for accessing naming and directory services in Java applications. Naming service connects names with objects, so that we can access objects with names. Directory service is also a naming service. Objects not only have names, but also properties. In Tomcat, JNDI can be used to define data sources and configuration information for the separation of development and deployment.
  1. Connector

A Connector will listen to client requests on a specified port, receive tcp connection requests sent by the Browser, create a Request and Response object to exchange data with the requester respectively, and then generate a thread to process the Request and pass the generated Request and Response objects to the processing Engine (part of the Container), Get the Response from the Engine and return it to the customer. There are two classic connectors in Tomcat, one directly listens to HTTP requests from the Browser and the other from other WebServer requests. The HTTP/1.1 Connector listens for HTTP requests from the client Browser at port 8080, and the AJP/1.3 Connector listens for Servlet/JSP requests from other web servers (other HTTP servers) at port 8009. The most important function of the Connector is to receive the connection Request and then allocate threads to the Container to process the Request. Therefore, this must be multi-threaded, and multi-threaded processing is the core of the Connector design.

  1. Container container

Container is the parent interface of the container. The container is designed using a typical responsibility chain design pattern. It is composed of four self container components, namely Engine, Host, context and Wrapper. These four components are responsible and contain. Usually, a Servlet class corresponds to a Wrapper. If there are multiple servlets defining multiple wrappers, if there are multiple wrappers, define a higher container, such as context. Context can also be defined in the parent container Host. A Host can correspond to multiple contexts. The Host is not necessary, but it is necessary to run the war program, because there must be a web.xml file in the war, and the Host is required for the parsing of this file. If there are multiple hosts, a top container Engine must be defined. The Engine has no parent container. An Engine represents a complete Servlet Engine.

  • The Engine container is relatively simple. It only defines some basic relationships.
  • The Host container is a sub container of the Engine. A Host represents a virtual Host in the Engine. The role of this virtual Host is to run multiple applications. It is responsible for installing and expanding these applications and identifying the applications so that they can be distinguished. Its sub container is usually
  • Context, which not only associates with sub containers, but also saves the information that a Host should have. The context container represents the context of the servlet. It has the basic environment for running the servlet. Theoretically, the servlet can run as long as there is a context. Simple Tomcat can have no Engine and Host. The most important function of context is to manage its servlet instances. Servlet instances appear as wrappers in context. Another point is how context finds the correct servlet to execute it. Tomcat5 was previously managed through a Mapper class. After tomcat5, this function was moved to request
  • The wrapper container represents a Servlet and is responsible for managing a Servlet, including the loading, initialization, execution and resource recovery of the Servlet. Wrapper is the bottom container. It has no child container, so the addChild calling it will report an error. The implementation class of wrapper is StandardWrapper. StandardWrapper also implements ServletConfig with a Servlet initialization information. It can be seen that StandardWrapper will directly deal with various information of Servlet.

  1. HTTP request process

The process of Tomcat Server processing an HTTP request:

Copy code

  1. When the user clicks the web page content, the request is sent to the local port 8080 and obtained by the Coyote HTTP/1.1 Connector listening there.
  2. The Connector sends the request to the Engine of the Service where it is located for processing, and waits for the Engine's response.
  3. The Engine obtains the request localhost/test/index.jsp to match all virtual Host hosts.
  4. The Engine matches the Host named localhost (even if it fails to match, the request is sent to the Host for processing, because the Host is defined as the default Host of the Engine). The Host named localhost obtains the request / test/index.jsp and matches all the contexts it has. The Host matches the Context with the path / test (if not, the request will be handed over to the Context with the path name "").
  5. The Context of path = "/ test" obtains the request / index.jsp in its mapping
    Find the corresponding Servlet in table. The Context matches the Servlet with URL PATTERN *. Jsp, which corresponds to the Servlet class of Jsp.
  6. Construct HttpServletRequest object and HttpServletResponse object, and call doGet() or doPost() of Servlet as parameters to execute business logic, data storage and other programs.
  7. Context returns the HttpServletResponse object after execution to the Host.
  8. The Host returns the HttpServletResponse object to the Engine.
  9. The Engine returns the HttpServletResponse object to the Connector.
  10. The Connector returns the HttpServletResponse object to the client Browser.
  1. server.xml configuration

    Let's take a look at a server.xml configuration example:
 1 plan: 
 2 Site page directory:/web/www      Domain name: www.test1.com 
 3 Forum page directory:/web/bbs     URL: bbs.test1.com/bbs 
 4 Website administrator: $CATALINA_HOME/wabapps   URL: manager.test.com    Allowed access address: 172.23.136.* 
 6 conf/server.xml 
 7 <Server port="8005" shutdown="SHUTDOWN"> 
 8   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> 
 9   <Listener className="org.apache.catalina.core.JasperListener" /> 
10   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> 
11   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> 
12   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> 
13   <GlobalNamingResources> 
14   <!-- Global named resources to define some external access resources, which is used to define the external resources referenced by all engine applications --!> 
15     <Resource name="UserDatabase" auth="Container" 
16               type="org.apache.catalina.UserDatabase" 
17               description="User database that can be updated and saved" 
18               factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
19               pathname="conf/tomcat-users.xml" /> 
20   </GlobalNamingResources> 
21   <!-- A definition called“ UserDatabase"Authentication resources, will conf/tomcat-users.xml Load into memory and authenticate in memory when authentication is required --> 
22   <Service name="Catalina"> 
23   <!-- # Define Service components and associate connectors and engines together. An Engine can correspond to multiple connectors, and there can only be one Engine in each Service --! > 
24     <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
25     <!-- modify HTTP/1.1 of Connector The listening port is 80.Requests accessed by the client through the browser can only be accessed through the browser HTTP Pass to tomcat.   --> 
26     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 
27     <Engine name="Catalina" defaultHost="test.com"> 
28     <!-- Modify current Engine,The default host is, www.test.com  --> 
29     <Realm className="org.apache.catalina.realm.LockOutRealm"> 
30         <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 
31                resourceName="UserDatabase"/> 
32     </Realm> 
33     # The Realm component defines the authentication of application access in the current container, which is authenticated through the external resource UserDatabase 
34       <Host name="test.com"  appBase="/web" unpackWARs="true" autoDeploy="true"> 
35       <!--  Define a host whose domain name is: test.com,The directory of the application is/web,Set automatic deployment and automatic decompression    --> 
36         <Alias>www.test.com</Alias> 
37         <!--    Define an alias www.test.com,similar apache of ServerAlias --> 
38         <Context path="" docBase="www/" reloadable="true" /> 
39         <!--    Define the application and access path"",Namely access www.test.com Can be accessed. The web page directory is: relative to appBase Lower www/,Namely/web/www,And when the application web.xml Or when there are relevant changes in classes, the current configuration will be automatically overloaded, that is, there is no need to restart tomcat Make the deployed new application effective  --> 
40         <Context path="/bbs" docBase="/web/bbs" reloadable="true" /> 
41         <!--  Define another independent application. The access path is: www.test.com/bbs,The application web page directory is/web/bbs   --> 
42         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/web/www/logs" 
43                prefix="www_access." suffix=".log" 
44                pattern="%h %l %u %t "%r" %s %b" /> 
45         <!--   Define a Valve Component to record tomcat The access log of is stored in the following directory:/web/www/logs If defined as a relative path, it is equivalent to $CATALINA_HOME,Not relative to appBase,Pay attention to this. Define the log file prefix as www_access.And with.log ending, pattern Define the format of log content. Specific fields indicate that you can view it tomcat Official documents   --> 
46       </Host> 
47       <Host name="manager.test.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> 
48       <!--   Define a host name man.test.com,The application directory is $CATALINA_HOME/webapps,Automatic decompression, automatic deployment   --> 
49         <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="172.23.136.*" /> 
50         <!--   Define remote address access policy, only 172 are allowed.23.136.*If the network segment accesses this host, others will be denied access  --> 
51         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/web/bbs/logs" 
52                prefix="bbs_access." suffix=".log" 
53                pattern="%h %l %u %t "%r" %s %b" /> 
54         <!--   Define the access log for this host      --> 
55       </Host> 
56     </Engine> 
57   </Service> 
58 </Server> 
60 conf/tomcat-users.xml 
61 <?xml version='1.0' encoding='utf-8'?> 
62 <tomcat-users> 
63   <role rolename="manager-gui" /> 
64   <!--  Define a role named: manager-gui    --> 
65   <user username="cz" password="manager$!!110" roles="manager-gui" /> 
66   <!--  Define a user's user name and password, and assign manager-gui Role of    --> 
67 </tomcat-users> 
  1. Tomcat log overview

    There are two types of logs: system log and console log.
    System logs mainly include running logs and access logs, which are divided into five categories: Catalina, localhost, manager, localhost_access and host manager. They are configured in the logging.properties file. Console logs include Catalina logs and logs output by programs (system print, console). Their log configuration can be output to the file catalina.out.

Log levels are divided into the following 7 types:

  SEVERE (highestvalue) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)

System log:

  1. catalina log: log file of catalina engine, file name catalina. Date. Log
  2. Localhost log: the log of exception thrown by the internal code under Tomcat. The file name is localhost. Date. Log
  3. localhost_access: by default, tomcat does not record access logs. Configuring Valve in the server.xml file enables tomcat to record access logs
  4. Manager, host Manager: the default manager application log under Tomcat

Console log:

In the Linux system, after Tomcat is started, a lot of information is written into the catalina.out file by default. We can track the operation of Tomcat and related applications through tail -f catalina.out. In windows, after starting Tomcat with startup.bat, we will find that the Catalina log is very different from the content recorded in Linux. Most of the information is only output to the screen without recording Record it in catalina.out. You can do this through setting.

Article transferred from

Tags: Java Tomcat server

Posted on Thu, 11 Nov 2021 03:51:10 -0500 by Chrisww