Browser Access Flow
HTTP request processing flow is as follows:
Browsers use HTTP protocol to access servers. HTTP is an application layer protocol used to define data communication format. Actual data transmission uses TCP/IP protocol.
- User enters URL: User enters in browser address bar such as https://www.example.com
- URL parsing: Browser decomposes URL into protocol, hostname, path, port
- DNS resolution: Browser queries local DNS server for IP address
- Establish TCP connection (three-way handshake)
- (HTTPS specific) TLS handshake
- Browser sends HTTP request
- Server processes request and returns response
- Browser renders page
Overall Architecture
General Flow
Tomcat is an HTTP server. When we use browser to initiate request to a website, sending HTTP request, the remote HTTP server receives this request and calls the specific program (Java class) to process. Often different requests are handled by different Java classes.
HTTP server receives request and passes it to Servlet container for processing. Servlet container calls business class through Servlet interface. This entire set of Servlet interface and Servlet container is called Servlet specification.
Tomcat both implements Servlet container according to Servlet specification requirements and also has HTTP server functionality.
Container Processing Flow
When user accesses a URL resource:
- HTTP server encapsulates request information using ServletRequest object
- Further calls specific Servlet in Servlet container
- In step 2, after Servlet container gets request, finds corresponding Servlet based on URL and Servlet mapping relationship
- If Servlet not loaded yet, uses reflection to create this Servlet and calls Servlet’s init method to complete object initialization
- Then calls this specific Servlet’s service method to process request. Request processing result is encapsulated using ServletResponse object
- Returns ServletResponse object to HTTP server. HTTP server sends response to client
Overall Architecture
Through above explanation, we find Tomcat has two very important functions to complete:
- Interact with client browser, communicate via Socket, convert between byte stream and Request, Response objects
- Servlet container processes business logic
Tomcat designed two core components:
- Connector: Responsible for external communication, handling Socket connections, responsible for converting network byte stream to Request and Response objects
- Container: Loads and manages Servlet, and specifically processes Request requests
Connector Component Coyote
Coyote is the component name of the connector in Tomcat, an external interface. Clients establish connection, send requests, and receive responses through Coyote with the server.
- Coyote encapsulates underlying network communication (Socket request and response processing)
- Coyote completely decouples Catalina container (container component) from specific request protocol and IO operation method
- Coyote converts Socket input to封装 Request object, further encapsulates and hands to Catalina container for processing. After request processing completes, Catalina writes results to output stream through Response object provided by Coyote
- Coyote is responsible for specific protocol (application layer) and IO (transport layer) related content
Coyote Supported IO Models and Protocols
Before 8.0, Tomcat default used BIO I/O method, later changed to NIO. Whether NIO, NIO2, or APR, performance is better than previous BIO. If using APR, can even reach Apache HTTP Server performance.