Tomcat Connector

Today I started to extract a slim and simplified connector from the source code of Tomcat 4.x. Before I describe it, I will introduce Tomcat Connector 4.x in brief.

When you use Tomcat out of the box to run web applications, it is able to serve HTML pages without any additional configuration. The reason this works is because it comes configures with an HTTP connector that can handle requests from a user’s web browser. Because of this connector, Tomcat can function as a standalone web server, and serve static HTTML page in addition to handling servlets and JSP pages.

Tomcat Server Class Connector Impl/Name Description                          
Tomcat 3.x org.apache.catalina.connector.http10.HttpConnector HTTP/1.0 Connector Came standard and configured with Tomcat 3.x.
Tomcat 4.0 and 4.1 org.apache.catalina.connector.http.HttpConnector HTTP/1.1 Connector Came standard with 4.0 and 4.1. In Tomcat 4.0, this was the default connector.
Tomcat 4.1 org.apache.coyote.tomcat4.CoyoteConnector Coyote HTTP Connector This is the default connector for Tomcat 4.1. This connector will soon obsolete both connectors defined above: HTTP/1.0 and HTTP/1.1. It will also be backwards compatible with both Tomcat 4.x and Tomcat 3.x.

Check out for more connectors:

Here are the weak points of using Tomcat as a Web server for static contents.

Here we can see there are two protocols HTTP/1.1 and HTTP/1.0. You can refer to to see the three new features brought by HTTP 1.1 (Persistent connections/100 Continue status, etc.)

HttpConnector was the default connector for Tomcat 4.x however it has been replaced by CoyoteConnector since the late of 4.x which is faster and has better performance. Tomcat requires its connectors should satisfy below conditions:

1. Implement interface org.apache.catalina.Connector

2. Create an instance of org.apache.catalina.Request

3. Create an instance of org.apache.catalina.Response

Connector should wait for HTTP requests, and create Request & Response objects, and then pass request object and response object to Servlet container.

The following diagram is the UML class diagram I generated from the source code of Tomcat 6.0.37. You can see it is using CoyoteConnector.


Basically you don’t need to bother about those utility classes. Nothing much. They are just wrappers from basic Java javax.servlet.http.* packages. For example,

public class Request implements HttpServletRequest {

public class Response implements HttpServletResponse {

public class CoyoteInputStream extends ServletInputStream {

Let’s come back to the slim connector I extracted for Duke Web Server. Below is its UML class diagram.

Connector (1)

Let’s explore its logic:

1. Boostrap will use HttpConnector to start the Socket.

2. HttpConnector invokes HttpProcessor to handle the request.

3. HttpProcessor implements interfaces Runnable so that it can be run in asynchronous. For each establishment, HttpProcessor instantiates one HttpRequest and one HttpResponse. HttpRequest and HttpResponse implements HttpServletRequest & HttpServletResponse respectively. Both of the classes have their facade class (façade pattern).

4. HttpRequest provides methods to handle its input stream (HttpRequestStream, extends from ServletInputStream.). Similar HttpResponse provides methods to handle its output stream (HttpResponseStream, extends from ServletOutputStream.)

5. If the request URI contains string ‘servlet’ – it means it’s a Servlet request, then HttpProcessor will invoke methods provides by HttpRequest and HttpResponse to process the header/request information, and then invoke ServletProcessor to return a HttpResponseFacade object to Servlet container.

If the request URI implies its a static page, like html, then it will invoke ‘StaticResourceProcessor’ to write the HTML content to response.

You can also refer to book ‘How Tomcat Works‘ (Chapter 3 & 4) for more details – It is a very awesome book. I strongly recommend you should buy one if you want to know how Tomcat works!

If you are interested in how to implement a simple pure Perl Web server, you can refer to Camel – A pure Perl web server.