(二)Tomcat源码阅读:明晰大致项目结构
创始人
2025-05-31 22:22:39

一、官网寻找项目结构文档

通过官方文档可以项目的主要组件介绍在 Apache Tomcat Server Configuration Reference中,因此我们点击进去看看。

Apache Tomcat Server Configuration Reference

二、排除一些不必要的内容

官方文档的内容太多,因此我们不可能每个东西都读,因此我们想要从大方向上排除掉一些不必要的东西,因此我排除掉了集群和其它板块,因为这两个板块不太重要。

 三、厘清大致结构

由图可知tomcat顶级的两个元素是Server 和Service 接下来是Executor ,Connector ,ContainerNested Components。但是即使到了这里,我们对项目结构还是不太了解,我们想要深入到各个组件中,才能通过各个组件的作用推出项目的结构。

进入项目介绍后,我们可以看到项目的概述。接下来我们来分析一下项目概述。

 项目概览比较重要的内容如下:配置主要可以归类为一下四类:(1)Top Level Elements:顶级元素是Server,Service内嵌在Server中,并连接多组Connectors,Connectors和Engine相关联。(2)Connectors :连接了客户端和Service。(3)Containers :Containers 是用来接收并返回请求的。一个Engine 处理来自于Service的请求,一个Host 处理来自于特定主机的请求,一个Context 处理来自于特定web应用的请求。(4)Nested Components:Nested Components可以嵌套在其它组件当中。

以下是文档中某些重要的关键点:(1)Introduction :项目概览。(2)Attributes :介绍了组件中的属性。(3)Nested Components:该组件内部嵌套的组件。(4)Special Features:介绍了一些特性。

Overview

This manual contains reference information about all of the configuration directives that can be included in a conf/server.xml file to configure the behavior of the Tomcat Servlet/JSP container. It does not attempt to describe which configuration directives should be used to perform specific tasks - for that, see the various How-To documents on the main index page.

Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive. Apache Ant-style variable substitution is supported; a system property with the name propname may be used in a configuration file using the syntax ${propname}. All system properties are available including those set using the -D syntax, those automatically made available by the JVM and those configured in the $CATALINA_BASE/conf/catalina.properties file.

The configuration element descriptions are organized into the following major categories:

  • Top Level Elements -  is the root element of the entire configuration file, while  represents a group of Connectors that is associated with an Engine.
  • Connectors - Represent the interface between external clients sending requests to (and receiving responses from) a particular Service.
  • Containers - Represent components whose function is to process incoming requests, and create the corresponding responses. An Engine handles all requests for a Service, a Host handles all requests for a particular virtual host, and a Context handles all requests for a specific web application.
  • Nested Components - Represent elements that can be nested inside the element for a Container. Some elements can be nested inside any Container, while others can only be nested inside a Context.

For each element, the corresponding documentation follows this general outline:

  • Introduction - Overall description of this particular component. There will be a corresponding Java interface (in the org.apache.catalina package) that is implemented by one or more standard implementations.
  • Attributes - The set of attributes that are legal for this element. Generally, this will be subdivided into Common attributes that are supported by all implementations of the corresponding Java interface, and Standard Implementation attributes that are specific to a particular Java class that implements this interface. The names of required attributes are bolded.
  • Nested Components - Enumerates which of the Nested Components can be legally nested within this element.
  • Special Features - Describes the configuration of a large variety of special features (specific to each element type) that are supported by the standard implementation of this interface.

(一)Server 

1、简介

关于Server 的介绍只有这短短的一句话,但是通过这句话我们可以知道Server 是tomcat项目中最顶层的元素。

Server element represents the entire Catalina servlet container. Therefore, it must be the single outermost element in the conf/server.xml configuration file. Its attributes represent the characteristics of the servlet container as a whole.

2、内嵌组件介绍

根据内嵌组件介绍我们可以知道:Server 中内嵌了Service 和GlobalNamingResources两个元素但是因为GlobalNamingResources涉及的技术JNDI已经过时,我们之后就不介绍它了。因此Service 只下辖了Server 这一个组件。

Nested Components

The following components may be nested inside a Server element:

  • Service - One or more service element.
  • GlobalNamingResources - Configure the JNDI global resources for the server.

(二)Service 

1、简介

这句话特别短但是信息量特别大,通过这句话,我们可以画出tomcat的大概结构。这句话是意思一个Service 由多个Connector 组成,并且这些Connector 共享同一个Engine,一个或者多个Service 可以内嵌到一个Server 中。

这里我们明确的项目结构是Service 从属于Server 。想要更深入的了解项目结构我们想要去阅读Connector 组件和Engine组件。读到这里大家相比也发现了规律,读源码比拼的不仅是代码能力,比拼的还是英文能力。

Service element represents the combination of one or more Connector components that share a single Engine component for processing incoming requests. One or more Service elements may be nested inside a Server element.

 2、内嵌组件介绍

根据这句话可以知道Service 组件中只能嵌套多个Connector 和唯一的Engine。

Nested Components

The only components that may be nested inside a Service element are one or more Connector elements, followed by exactly one Engine element.

(三)Connector 

1、简介

 Connector 中由三个协议,我们选择使用最广泛的HTTP 1.1协议的Connector 。

这段话的意思是  Connector 可以处理HTTP/1.1协议,并且可以处理servlets和JSP页面。它会监听server端口的连接数。Connector 可作为Service 的一部分。Connector 可以通过其相关联的Engine接收并响应请求。

每当有请求来时,Connector 都需要一个线程。如果请求过多,Connector 内部会创建线程知道达到了最大线程数,在这样的情况下如果还不断有请求过来,tomcat就会创建更多的Connector直到达到Connector上限。连接会在Connector 中排队,直到有线程释放可以处理当前连接。如果Connector最大数量还是突破了,将会调用系统的队列进行处理。如果系统队列还是满了tomcat将会拒绝接收消息。

从中我们可以知道Connector 是从属于Service 的,接下来我们来看看Engine。

The HTTP Connector element represents a Connector component that supports the HTTP/1.1 protocol. It enables Catalina to function as a stand-alone web server, in addition to its ability to execute servlets and JSP pages. A particular instance of this component listens for connections on a specific TCP port number on the server. One or more such Connectors can be configured as part of a single Service, each forwarding to the associated Engine to perform request processing and create the response.

Each incoming, non-asynchronous request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests are received, Tomcat will accept new connections until the current number of connections reaches maxConnections. Connections are queued inside the server socket created by the Connector until a thread becomes available to process the connection. Once maxConnections has been reached the operating system will queue further connections. The size of the operating system provided connection queue may be controlled by the acceptCount attribute. If the operating system queue fills, further connection requests may be refused or may time out.

  2、内嵌组件介绍

Connector 的内嵌组件没有关于项目结构的信息因此这里就不介绍了。

(四)Engine

1、简介

Engine 用来处理Service的所有请求并将响应返回给客户端。Engine 和Service是一一对应的关系,一个Engine 对应着多个Connectors。

Introduction

The Engine element represents the entire request processing machinery associated with a particular Catalina 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.

Exactly one Engine element MUST be nested inside a Service element, following all of the corresponding Connector elements associated with this Service.

  2、内嵌组件介绍

由该介绍可知Engine 内嵌了HostRealm但是因为Realm技术已经过时,我们之后便不介绍它了。接下来我们就应该去学习Host

Nested Components

You can nest one or more Host elements inside this Engine element, each representing a different virtual host associated with this server. At least one Host is required, and one of the nested Hosts MUST have a name that matches the name specified for the defaultHost attribute, listed above.

You can nest at most one instance of the following utility components by nesting a corresponding element inside your Engine element:

  • Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Hosts and Contexts nested inside this Engine, unless overridden by a Realm configuration at a lower level.

(五)Host

1、简介

这段介绍太长,概括来说Host 代表了一个虚拟主机例如:www.mycompany.com之类的,并且其内嵌了  Context这个组件

The Host element represents a virtual host, which is an association of a network name for a server (such as "www.mycompany.com") with the particular server on which Tomcat is running. For clients to be able to connect to a Tomcat server using its network name, this name must be registered in the Domain Name Service (DNS) server that manages the Internet domain you belong to - contact your Network Administrator for more information.

In many cases, System Administrators wish to associate more than one network name (such as www.mycompany.com and company.com) with the same virtual host and applications. This can be accomplished using the Host Name Aliases feature discussed below.

One or more Host elements are nested inside an Engine element. Inside the Host element, you can nest Context elements for the web applications associated with this virtual host. Exactly one of the Hosts associated with each Engine MUST have a name matching the defaultHost attribute of that Engine.

Clients normally use host names to identify the server they wish to connect to. This host name is also included in the HTTP request headers. Tomcat extracts the host name from the HTTP headers and looks for a Host with a matching name. If no match is found, the request is routed to the default host. The name of the default host does not have to match a DNS name (although it can) since any request where the DNS name does not match the name of a Host element will be routed to the default host.

The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

  2、内嵌组件介绍

由此可知Host 内部内嵌了多个Context组件

Nested Components

You can nest one or more Context elements inside this Host element, each representing a different web application associated with this virtual host.

You can nest at most one instance of the following utility components by nesting a corresponding element inside your Host element:

  • Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Contexts nested inside this Host (unless overridden by a Realm configuration at a lower level).

(六)Context 

1、简介

概括来讲Context 组件主要是一个web应用的容器,并且在CATALINA_BASE对其进行配置。Context 从属于Host 。

Introduction

The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

The Context element represents a web application, which is run within a particular virtual host. Each web application is based on a Web Application Archive (WAR) file, or a corresponding directory containing the corresponding unpacked contents, as described in the Servlet Specification (version 2.2 or later). For more information about web application archives, you can download the Servlet Specification, and review the Tomcat Application Developer's Guide.

The web application used to process each HTTP request is selected by Catalina based on matching the longest possible prefix of the Request URI against the context path of each defined Context. Once selected, that Context will select an appropriate servlet to process the incoming request, according to the servlet mappings defined by the web application deployment.

You may define as many Context elements as you wish. Each such Context MUST have a unique context name within a virtual host. The context path does not need to be unique (see parallel deployment below). In addition, a Context must be present with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.

 2、内嵌组件介绍

Context的组件有:Cookie Processor,Loader,Manager,Realm,Resources,WatchedResource,JarScanner。但是这些组件我们都没必要太了解因为官网说了,这些组件直接用默认的效率会更高,因此我们没有必要研究这些组件了。

Nested Components

You can nest at most one instance of the following utility components by nesting a corresponding element inside your Context element:

  • Cookie Processor - Configure parsing and generation of HTTP cookie headers.
  • Loader - Configure the web application class loader that will be used to load servlet and bean classes for this web application. Normally, the default configuration of the class loader will be sufficient.
  • Manager - Configure the session manager that will be used to create, destroy, and persist HTTP sessions for this web application. Normally, the default configuration of the session manager will be sufficient.
  • Realm - Configure a realm that will allow its database of users, and their associated roles, to be utilized solely for this particular web application. If not specified, this web application will utilize the Realm associated with the owning Host or Engine.
  • Resources - Configure the resource manager that will be used to access the static resources associated with this web application. Normally, the default configuration of the resource manager will be sufficient.
  • WatchedResource - The auto deployer will monitor the specified static resource of the web application for updates, and will reload the web application if it is updated. The content of this element must be a string.
  • JarScanner - Configure the Jar Scanner that will be used to scan the web application for JAR files and directories of class files. It is typically used during web application start to identify configuration files such as TLDs o web-fragment.xml files that must be processed as part of the web application initialisation. Normally, the default Jar Scanner configuration will be sufficient.

由此这些嵌套我们也不需要学习了,因为官网已经说明了这些东西使用默认配置效率更高,正常情况下不需要我们动它。因此我们接下来学习的组件就只有Listener ,Filters 和Valve 

 (七)Listener 

1、简介

Listener 定义了一个组件发生的特定事件例如:启动或者停止。Listener 可以内嵌在Server, Engine, Host 和Context中。

Introduction

Listener element defines a component that performs actions when specific events occur, usually Tomcat starting or Tomcat stopping.

Listeners may be nested inside a Server, Engine, Host or Context. Some Listeners are only intended to be nested inside specific elements. These constraints are noted in the documentation below.

 2、内嵌组件介绍

无内嵌组件。

(八)Valve 

1、简介

多个Valve 可以通过内嵌在容器中的方式,组成流水线。

Introduction

Valve element represents a component that will be inserted into the request processing pipeline for the associated Catalina container (Engine, Host, or Context). Individual Valves have distinct processing capabilities, and are described individually below.

The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

(九)Filters 

1、简介

Filters 可以被作用于web应用。web应用内嵌在context中由此可知Filters 在context中,并充当最后一道阀门的作用。

Introduction

Tomcat provides a number of Filters which may be configured for use with all web applications using $CATALINA_BASE/conf/web.xml or may be configured for individual web applications by configuring them in the application's WEB-INF/web.xml. Each filter is described below.

This description uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

四、Tomcat大体结构

由上面对各个组件的学习,我们可以知道Tomcat的大致结构图如下。接下来的学习,我们会围绕着上面八大组件展开学习。

相关内容

热门资讯

美单边关税让全球经济面临更大不... 美国征收关税的对象和标准可能随意变更,其关税政策具有不可预测性。无论是外国企业,还是美国本土企业,都...
桃李面包创始人向其两儿子转让3... 5月30日晚间,桃李面包(沪市代码:603866)公告称,公司控股股东及实际控制人吴志刚通过大宗交易...
前4月东莞重大项目完成投资42... 本期看点:前4月东莞重大项目完成投资429.09亿元;长联科技募投项目提升年产至2.77万吨;广东省...
恒生指数跌幅扩大至2%,医药、... 6月2日,恒生指数跌幅扩大至2%,医药、地产、能源板块跌幅居前,美中嘉和跌超14%,石四药集团跌近1...
港股、A50飘绿,亚太市场多数... 早间,亚太市场多数下跌。港股、A50集体下跌其中,恒生指数、恒生科技指数开盘跌幅扩大, 港股生物技术...
雷军看好的两兄弟,要IPO了 ... 2021年夏,小米产业园办公室内,雷军饶有兴致地打量眼前一对兄弟,“为什么张波是创始人,董事长却是张...
圣阳股份涨1.66%,成交额9... 5月30日,圣阳股份涨1.66%,成交额9.82亿元,换手率15.03%,总市值66.67亿元。 异...
“龙舟溪游”点燃江南西商圈消费... 5月31日至6月2日,海珠区江南中街道一年一度的节假日消费体验活动如约而至。今年“龙舟溪游・与你童在...
恒生指数开盘跌1.06%,恒生... 6月2日,恒生指数开盘跌1.06%报23043.10点,恒生科技指数跌1.33%,恒生中国企业指数跌...
*ST天喻实控人被刑事立案 此... 5月30日,*ST天喻(300205.SZ)发布公告称,公司收到武汉市公安局送达的《立案告知书》,公...
“玩”出更多花样——“六一”礼... 能交流、讲故事的毛绒玩偶,融入中国文化、科技元素的拼插积木,电影《哪吒2》衍生开发的各类公仔……“六...
5月十大牛股出炉:中邮科技逾1... 截至5月30日收盘,沪指月内累计涨2.09%,深证成指累计涨1.42%,创业板指累计涨2.32%。在...
恒生指数止步周线七连阳,IPO... 南方财经全媒体记者 袁思杰 实习生武桐羽 香港报道上周(5月26日-5月30日),港股震荡回调,主要...
新势力车企5月放榜:零跑汽车登... 近期,国内多家主流自主汽车企业纷纷发布了5月份的销售数据。造车新势力5月交付成绩也出炉,第一名依然是...
最新!2025新势力5月销量出... 5月新势力表现如何?2025年5月的销售周期刚刚过去,不少造车新势力厂商的销量数据,已经新鲜出炉。数...
两家A股公司,收终止上市决定 又有两家A股上市公司收到股票终止上市决定,6月10日进入退市整理期。 上述自律监管决定书指出,因2...
重磅,事关教育强国,主力提前埋... 数据是个宝数据宝投资少烦恼这些产业的景气度处于上升期。《求是》杂志发表文章《加快建设教育强国》6月1...
阳光诺和“二刷”收购 80后富... 《投资者网》蔡俊时隔2年后,阳光诺和(688621.SH,下称“公司”)再拟收购同一个资产。实际上,...
买车,不安全了? 买车,不安全... 在新能源汽车市场竞争空前激烈的当下,车企、经销商习惯于采取更加激进的营销、市场策略,尤其在行业加速“...
欧佩克+同意7月再增产41.1... 为了增产惩罚超产国并争夺市场份额,欧佩克+连续第三个月大幅增产,美国页岩油生产商或首当其冲,美油一度...
经济学泰斗菲舍尔逝世:培育伯南... 当地时间6月1日,以色列央行发布声明称,世界著名经济学家、以色列央行前行长及美联储前副主席菲舍尔(S...
更名!“天府证券”来了 天府证... 【导读】宏信证券更名为天府证券中国基金报记者 吴君这家券商,历史上第二次更名。5月末,工商信息显示,...
两家A股公司,收终止上市决定 ... 又有两家A股上市公司收到股票终止上市决定,6月10日进入退市整理期。*ST鹏博(600804)公告称...
瑞幸降价迈入“6块9”时代?瑞... 说起最近几年的咖啡茶饮市场,相信每个人都不会陌生,各家咖啡茶饮企业的各种降价消息是此起彼伏,就在最近...
主次节奏:6.1黄金 - 每周... 本文每周初更新发布梳理各级别走势分析和预期主次节奏:做有品质的三方服务黄金月线图(超长线) 月线图...
超400亿资金狂涌!这类ETF... 债券ETF市场持续扩容。今年以来,债券市场表现震荡,债券类基金回报远不及预期,但这并未妨碍债券型ET...
坚定信心 行稳致远(记者手记) 侯琳良 最近一段时间,海尔集团上世纪90年代投资制作的《海尔兄弟》动画片,在多个视频平台上线高清重制...
世纪大辩论2——哈耶克与凯恩斯... 本来节后决定启动一个项目,但家里临时有事,需要陪家人去一趟北京,节后拉群的事,因此要推迟一周左右(具...
4月广州消费品市场表现强劲 1-4月,随着消费品以旧换新等促消费政策持续发力和各类会展活动陆续开展,政策相关消费快速增长,升级类...
金价,又跌了! 人民财讯5月31日电,5月30日,COMEX黄金期货收跌0.92%,报3313.1美元/盎司。 从高...