Tomcat architecture diagram:

The Connector role

Connect receives external connection requests, creates Request and Response objects for data exchange, and allocates threads for Container to handle the requests.

Connector can support multiple protocol requests (such as HTTP and AJP). The configuration of Connector is described in conf/server.xml.

Coyote is the name of Tomcat’s connector framework, an external interface provided by the Tomcat server for clients to access. The client establishes a connection with the server through Coyote, sends a request, and receives a response.

Coyote encapsulates the underlying network communication (Socket request and response processing) and provides a unified interface for Catalina, completely decoupling Catalina containers from specific request protocols and IO operation modes.

Coyote converts the Socket input into a Request object and sends it to Catalina for processing. After processing the Request, Catalina writes the result into the output stream through the Response object provided by Coyote.

Coyote, as an independent module, is only responsible for specific protocol and IO operations, which is not directly related to the implementation of Servlet specification. Therefore, Request and Response objects do not implement the corresponding interface of Servlet specification. Instead, they are further encapsulated in the Catalina container as ServletRequest and ServletResponse.

IO model

IO models supported by Tomcat (since version 8.5/9.0, Tomcat has removed BIO support)

  • NIO: Non-blocking I/O, implemented using the Java NIO class library.
  • NIO2: Asynchronous I/O, using JDK7 NIO2 class library implementation.
  • APR: Implemented by Apache Portable runtime, is a native library written in C/C++. If you choose this option, you will need to install the APR library separately.

Application layer protocol

  • HTTP/1.1: This is the access protocol used by most Web applications.
  • AJP: For integration with Web servers (such as Apache) for static resource optimization and cluster deployment. Currently supports AJP/1.3.
  • HTTP/2: HTTP 2.0 dramatically improves Web performance. The next generation HTTP protocol, supported since versions 8.5 and 9.0.

Before 8.0, Tomcat used BIO as the default I/O mode, then NIO. NIO, NIO2 and APR are superior to BIO in performance. With APR, you can even achieve the performance of Apache HTTP Server.

Connector assembly

The role of the individual components in the connector:

EndPoint

EndPoint: The Coyote communication EndPoint, that is, the communication listening interface, is the specific Socket receiving and sending processor, is the transport layer abstraction, EndPoint is used to implement TCP/IP protocol.

Tomcat does not have an EndPoint interface. Instead, it provides an abstract class AbstractEndpoint that defines two internal classes: Acceptor and SocketProcessor.

Acceptor is used to listen for Socket connection requests.

SocketProcessor processes the received Socket request. It implements the Runnable interface and invokes the protocol Processor in the Run method to process the Socket request. To increase processing power, the SocketProcessor is committed to the thread pool for execution.

Processor

Processor: Processor processes the Coyote protocol. While the EndPoint is used to implement TCP/IP, the Processor is used to implement HTTP.

The Processor receives the Socket from the EndPoint, reads the byte stream, parses it into Tomcat Request and Response objects, and submits them to the container for processing through the Adapter.

Processor is an abstraction of application-layer protocols.

ProtocolHandler

ProtocolHandler: Indicates a Coyote protocol interface. This interface enables protocol-specific processing through the Endpoint and Processor.

Tomcat provides six implementation classes by protocol and I/O: AjpNioProtocol AjpAprProtocol AjpNio2Protocol Http11NioProtocol Http11Nio2Protocol Http11AprProtocol.

When you configure tomcat/conf/server.xml, you need to specify at least a specific ProtocolHandler and, of course, a protocol name.

For example: HTTP/1.1, if APR is installed, Http11AprProtocol will be used, otherwise Http11NioProtocol will be used.

Adapter

Due to different protocols, the Request information sent by clients varies. Tomcat defines its own Request class to store the Request information.

The ProtocolHandler interface is responsible for resolving requests and generating Tomcat Request classes.

However, the Request object is not a standard ServletRequest, which means that the Container cannot be called with a Tomcat Request as an argument.

The Tomcat designer’s solution was to introduce CoyoteAdapter, a classic use of the adapter pattern. The connector calls the Sevice method of CoyoteAdapter, passing in a Tomcat Request object, The CoyoteAdapter is responsible for turning the Tomcat Request into a ServletRequest and then calling the container’s Service method.