一、整體說明
tomcat需要完成兩件事情:
- 接受外部HTTP請求
- 加載servlet,并且把請求傳給Servlet進行處理,
整體可以抽象為如下:

二、整體代碼架構
在tomcat的代碼中抽象了以下三個類:
- Server類,代表tomcat實體
- Connector類,代表HTTP監聽器(上圖中的HttpServer)
-
A "Connector" represents an endpoint by which requests are received and responses are returned
- 多個connector代表監聽后處理模式不同,比如bio,nio等,
-
- Container類,代表Servlet容器
另外還抽象了一個Service類,用來包裝Connector和Container類,
A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level
其中
- 一個Server可以有多個Service【實際應用場景中,一般來說只有一個】,
- 一個Service可以對應多個Connector【實際應用場景中,一般來說只有一個】,
- 一個Service對于一個Container,
整體代碼架構如下:

上圖中Connector監聽到請求后最終把HTTP(TCP)請求轉換成了Servlet請求傳給Container,
三、Service類分析
tomcat中Service類的實作類是StandardService類

Service介面核心代碼
public interface Service { public Engine getContainer(); public void setContainer(Engine engine); public Server getServer(); public void setServer(Server server); public void addConnector(Connector connector); public Connector[] findConnectors(); public void removeConnector(Connector connector); }
可以看到Service主要負責調度Connector和Container,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/156820.html
標籤:Java
上一篇:如何打包USB驅動
