Inside Tomcat: Architecture, Lifecycle, Connectors, and Advanced Features
This article provides a comprehensive overview of Tomcat, covering its role as an open‑source Java web server, the internal components such as Server, Service, Connector, and Container, lifecycle management, servlet and JSP processing, connector types, pipeline/valve mechanisms, Comet push, and asynchronous servlet support.
What is Tomcat?
Tomcat is an open‑source Java Web application server that implements part of the Java EE specifications such as Servlet, JSP, JSTL and WebSocket.
Servlet Container Components
Tomcat consists of a Server, one or more Services, Connectors, Containers (Catalina), Loaders, Realms, JMX, Jasper, Session manager, Pipeline, Naming, etc.
Server : the whole Tomcat instance, manages Services and listens on the shutdown port.
Service : groups a Connector and a Container; each Service runs in the same JVM.
Connector : receives external requests (HTTP, AJP) and passes them to the Container.
Container (Catalina) : the servlet container, contains Engine, Host, Context, Wrapper hierarchy.
Loader : wraps a Java ClassLoader for loading classes.
Realm : provides authentication and role mapping.
JMX : enables remote monitoring of Tomcat.
Jasper : JSP compilation engine.
Session : manages HTTP sessions, supports persistence and clustering.
Pipeline : a chain of Valves that process requests and responses.
Naming : JNDI support for resources such as data sources.
Container Hierarchy
Engine – top‑level container, may contain multiple Hosts.
Host – virtual host that holds Contexts (web applications).
Context – represents a single web application, contains Wrappers.
Wrapper – the lowest level, wraps a Servlet instance.
Lifecycle Management
Tomcat defines twelve lifecycle states (New, Initializing, Initialized, Starting, Started, Stopping, Stopped, Destroying, Destroyed, Failed, etc.). Components implement initInternal, startInternal, stopInternal, destroyInternal to react to state changes.
Servlet Lifecycle
Request arrives and is mapped to a Servlet.
If the Servlet instance does not exist, Tomcat creates it and calls init.
Tomcat creates Request and Response objects and invokes the Servlet’s service method (which dispatches to doGet, doPost, etc.).
When the application shuts down, Tomcat calls destroy.
The load-on-startup attribute controls when a Servlet is instantiated, and the deprecated singleThreadModel should be avoided.
Request Processing Flow
Connector listens on a configured port.
Connector creates Request/Response and invokes the top‑level Pipeline.
Pipeline’s Valves (including the basic Valve) process the request, eventually reaching the target Servlet.
Connector writes the response back to the socket.
Pipeline and Valve
A Pipeline is analogous to a pipe; each Valve is a filter in the chain. The basic Valve is mandatory; other Valves are optional. Valves are linked via setNext and invoke.
JSP Engine
Jasper compiles JSP pages into Servlets. JSP lifecycle includes compilation, initialization, execution, and destruction. JSP elements include scriptlets, declarations, expressions, directives, actions, and implicit objects such as request, response, session, etc.
Connector Types
Tomcat supports three connector implementations:
BIO – blocking I/O based on java.io.
NIO – non‑blocking I/O using java.nio and selector‑based multiplexing.
APR – native I/O via Apache Portable Runtime (JNI).
Comet (Server Push)
Comet enables server‑initiated data push before WebSocket existed. Implemented by extending HttpServlet and implementing CometProcessor with events: Begin, Read, End, Error.
Asynchronous Servlets
Async Servlets allow the request‑handling thread to be released while processing continues in another thread.
Client sends a request.
Servlet container allocates a thread and the servlet calls request.startAsync(), obtaining an AsyncContext and returning.
The original thread exits; the response remains open.
A business thread uses the saved AsyncContext to complete the response.
Async listeners include onStartAsync, onComplete, onError, and onTimeout. After onError or onTimeout, onComplete is invoked and the request/response can no longer be used.
NIO Processing Details
Acceptor threads accept connections and register them with a selector. Poller threads retrieve events, create Http11NioProcessor instances, and hand them to a thread pool. The CoyoteAdapter adapts Connector calls to the Container pipeline.
Original source: Juejin article (https://juejin.cn/post/6844903473482317837)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
