Apache Tomcat (1): Core Components and Their Interactions

Jimin
5 min readMar 13, 2024

--

As a DevOps engineer, navigating the complexities of modern web applications and server management is part of the daily grind. However, since I didn’t have a chance to study it deeply, I’ve always felt a gap in my understanding of Apache Tomcat’s intricacies. Recognizing this as a limitation, I decided to learn Apache Tomcat thoroughly. This blog series is a record of that journey, aimed not only at deepening my own understanding but also at assisting others who might find themselves in a similar situation.

What is Apache Tomcat?

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages (JSPs), and Java Expression Language (EL) technologies. Tomcat provides a “pure Java” HTTP web server environment for Java code to run in. It is a project of the Apache Software Foundation, developed and maintained by an open community of developers under the Apache license.

Tomcat serves as a servlet container that executes Java servlets and renders web pages that include JSP coding. Despite being much more than just a web server, its capabilities to handle large-scale projects make it a go-to choice for developers and organizations worldwide.

Core Components of Tomcat

Understanding Tomcat’s architecture is essential for effective management and deployment. Here are the key components:

  1. Coyote (The HTTP Connector)
    - Imagine Coyote as the gatekeeper of Tomcat. It listens for incoming HTTP requests (those messages from web browsers) and directs them to the right place.
    - Coyote handles the low-level network communication, ensuring that requests find their way into Tomcat’s heart.
  2. Catalina (The Servlet Engine)
    - Catalina is the powerhouse of Tomcat. It’s where the real magic happens.
    - When a request arrives, Catalina decides whether it’s a servlet or a JSP (JavaServer Page). If it is a servlet, Catalina takes charge.
    - Catalina manages the lifecycle of servlets, including loading, initialization, and handling requests.
  3. Jasper (The JSP Engine)
    - JSPs are dynamic web pages with embedded Java code. But how do they become something browsers can understand?
    - That’s where Jasper comes in. It’s the JSP engine within Tomcat.
    - Jasper compiles JSP files into Java bytecode. Think of it as translating JSPs into a language that Catalina can sync with.
    - Once compiled, Jasper hands over the Java bytecode to Catalina for execution.
  4. Valve is a component that allows you to intercept requests in Tomcat, providing services like logging, security checks, and request filtering.
  5. Cluster
    Facilitates Tomcat’s ability to run in a clustered environment, providing load balancing and failover capabilities for high availability.

How Tomcat Handles Requests

+-------------------+    +-----------------+      +--------------------+
| |--->| | ---> | |
| Client (Browser) | | Coyote (HTTP | | Servlet Engine |
| |<---| Connector) | <--- | (Catalina) |
+-------------------+ +-----------------+ +--------------------+
HTTP Request Acceptor Threads Servlets/JSPs
HTTP Response Worker Threads Processing
SSL/TLS Encryption
(if HTTPS)
| ^
| |
v |
+-------------------+
| |
| Jasper (JSP |
JSP Compilation (if JSP file) and | Engine, if JSP) |
Delivery of Java bytecode for Catalina to process | |
+-------------------+
  1. Client Request (User Clicks a Link)
    - Imagine you click a link on a website or type a URL in your browser.
    - This action sends an HTTP request to the web server.
  2. Coyote (The Gatekeeper)
    - Meet Coyote, our gatekeeper. It checks the request’s format and ensures it’s valid.
    - If everything looks good, Coyote lets the request in.
  3. Catalina (The Decision Maker)
    -
    Catalina is like the manager. It decides what to do with the request.
    - If it’s a servlet (a Java program), Catalina takes charge.
    - If it’s a JSP (JavaServer Page) (a mix of HTML and Java), Jasper (our translator) steps in.
  4. Servlets (The Doers)
    If it’s a servlet:
    - Catalina finds the right servlet (like picking the right tool for the job).
    - The servlet processes the request: reads data, talks to databases, and does calculations.
    - Finally, it generates an HTTP response.
  5. JSPs (The Dynamic Pages)
    If it’s a JSP:
    - Jasper compiles it into Java code.
    - The compiled JSP becomes part of the servlet team.
    - The rest is similar to servlet handling: creating a response and sending it back.
  6. The Encore (Sending the Response)
    - Catalina wraps up by sending the response back to your browser.
    - Your browser displays the web page, and you see the result.

Tomcat Architecture Elements

Server (Tomcat Instance)
|
+-- Service (1)
| |
| +-- Engine (1)
| |
| +-- Host (1)
| | |
| | +-- Context (1): web app
| | +-- Context (2)
| |
| +-- Host (2)
|
+-- Connector (1)
+-- Connector (2)
|
+-- Service (2)
|
+-- Engine (1)
|
+-- Host (1)
| |
| +-- Context (1): web app
| +-- Context (2)
|
+-- Host (2)
  1. Server
    - Represents the whole Tomcat instance.
    - The top-level element in server.xml.
    - Can contain multiple Services.
  2. Service
    -
    Groups one or more Connectors to a single Engine.
    - Acts as a conduit between the network and the servlet processing.
  3. Engine
    -
    Responsible for the request processing pipeline of a Service.
    - Directs requests to the appropriate Host.
  4. Host
    -
    Represents a virtual host/domain within Tomcat.
    - Contains multiple Contexts (web applications).
  5. Context
    -
    Encapsulates an individual web application.
    - Where servlets and JSPs live and execute.

How they relate

https://medium.com/@nikhilmanikonda/tomcat-who-i-am-and-what-i-do-e91ff72fb2ea
  • Catalina (Servlet Container)
    -
    Covers the Server, Service, Engine, Host, and Context elements.
    - Manages servlets and JSPs, providing the runtime environment for them.
  • Coyote (Connector)
    -
    Functions as the Connector component within a Service.
    - Handles client communication, managing input and output through network protocols like HTTP and AJP.
    - Each Service in Tomcat utilizes Coyote connectors to receive requests from clients and pass them to the associated Engine for processing..
  • Jasper (JSP Engine)
    -
    Associated with the Context element.
    - Compiles JSPs into servlets, allowing them to be managed by Catalina.
  • Cluster
    -
    Provides clustering and session replication capabilities.
    - Works in conjunction with the Engine or Service for high availability.
  • Valve
    -
    Pluggable components for intercepting requests at any point in the processing pipeline.
    - Can be attached at the Server, Service, Engine, Host, or Context levels.

References

  1. Apache Tomcat. (n.d.). Tomcat Architecture Overview. Retrieved from https://tomcat.apache.org/tomcat-9.0-doc/architecture/overview.html
  2. Nikhil Manikonda. (Nov 2019). “Tomcat: Who I Am and What I Do.” Medium. Available at: https://medium.com/@nikhilmanikonda/tomcat-who-i-am-and-what-i-do-e91ff72fb2ea.
  3. Josh Samuelson. (May 2019). “Learning Apache Tomcat.” LinkedIn. Available at: https://www.linkedin.com/learning/learning-apache-tomcat.
  4. Victor Lee. (Dec 2015). “톰캣 운영 노하우” Slideshare. Available at: https://www.slideshare.net/jieunsys/ss-56543446.

--

--

Jimin

DevOps engineer and tech enthusiast. Sharing tech insights to simplify the complex. Let's connect on LinkedIn! https://www.linkedin.com/in/byun-jimin/