Inside Tomcat: Unveiling Its Core Architecture and Lifecycle
This article explores Tomcat’s modular design, detailing the roles of Service, Connector, Container, Server, and Lifecycle interfaces, and illustrates how requests flow through the servlet engine with code examples and component diagrams.
Tomcat Overall Structure
Tomcat’s architecture is modular, with the core components Service and Container forming the heart of the server. A Service groups one or more Connectors with a single Container, while the Server controls the lifecycle of all Services.
Service as a "Marriage"
The Service acts like a marriage certificate linking Connectors (the "boy") and Containers (the "girl"). A Service can have multiple Connectors but only one Container.
Service Interface
Key methods include adding Connectors and managing their lifecycle.
StandardService.setContainer
public void setContainer(Container container) { Container oldContainer = this.container; if ((oldContainer != null) && (oldContainer instanceof Engine)) ((Engine) oldContainer).setService(null); this.container = container; if ((this.container != null) && (this.container instanceof Engine)) ((Engine) this.container).setService(this); if (started && (this.container != null) && (this.container instanceof Lifecycle)) { try { ((Lifecycle) this.container).start(); } catch (LifecycleException e) { ; } } support.firePropertyChange("container", oldContainer, this.container); }StandardService.addConnector
public void addConnector(Connector connector) { synchronized (connectors) { connector.setContainer(this.container); connector.setService(this); Connector results[] = new Connector[connectors.length + 1]; System.arraycopy(connectors, 0, results, 0, connectors.length); results[connectors.length] = connector; connectors = results; if (initialized) { try { connector.initialize(); } catch (LifecycleException e) { e.printStackTrace(System.err); } } } if (started && (connector instanceof Lifecycle)) { try { ((Lifecycle) connector).start(); } catch (LifecycleException e) { ; } } support.firePropertyChange("connector", null, connector); }Connector Component
The Connector receives TCP connections, creates Request and Response objects, and delegates processing to a Container.
HttpConnector.start
public void start() throws LifecycleException { if (started) throw new LifecycleException(sm.getString("httpConnector.alreadyStarted")); threadName = "HttpConnector[" + port + "]"; lifecycle.fireLifecycleEvent(START_EVENT, null); started = true; threadStart(); while (curProcessors < minProcessors) { if ((maxProcessors > 0) && (curProcessors >= maxProcessors)) break; HttpProcessor processor = newProcessor(); recycle(processor); } }HttpProcessor.assign
synchronized void assign(Socket socket) { while (available) { try { wait(); } catch (InterruptedException e) { } } this.socket = socket; available = true; notifyAll(); if ((debug >= 1) && (socket != null)) log(" An incoming request is being assigned"); }HttpProcessor.run
public void run() { while (!stopped) { Socket socket = await(); if (socket == null) continue; try { process(socket); } catch (Throwable t) { log("process.invoke", t); } connector.recycle(this); } }Servlet Container "Container"
Container is the parent interface for Engine, Host, Context, and Wrapper, forming a classic responsibility‑chain pattern.
Server.xml Example
<Context path="/library" docBase="D:\projects\library\deploy\target\library.war" reloadable="true" />Engine Container
Engine is the top‑level servlet engine; it can only contain Host children.
StandardEngine.addChild
public void addChild(Container child) { if (!(child instanceof Host)) throw new IllegalArgumentException(sm.getString("standardEngine.notHost")); super.addChild(child);
}Host Container
Host represents a virtual host within an Engine and contains Context children.
Context Container
Context provides the runtime environment for a web application and manages its Wrappers (Servlets).
StandardContext.start
public synchronized void start() throws LifecycleException { // initialization logic omitted for brevity lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); // start child containers and pipeline for (Container child : findChildren()) { if (child instanceof Lifecycle) ((Lifecycle) child).start(); } if (pipeline instanceof Lifecycle) ((Lifecycle) pipeline).start(); }StandardContext.backgroundProcess
public void backgroundProcess() { if (!started) return; if (getManager() != null && (count = (count + 1) % managerChecksFrequency) == 0) { try { getManager().backgroundProcess(); } catch (Exception x) { log.warn("Unable to perform background process on manager", x); } } if (getLoader() != null && reloadable && getLoader().modified()) { try { Thread.currentThread().setContextClassLoader(StandardContext.class.getClassLoader()); reload(); } finally { Thread.currentThread().setContextClassLoader(getLoader().getClassLoader()); } } }Wrapper Container
Wrapper represents a single Servlet, handling its loading, initialization, execution, and destruction.
StandardWrapper.loadServlet
public synchronized Servlet loadServlet() throws ServletException { // class loading omitted for brevity Servlet servlet = (Servlet) classClass.newInstance(); if (servlet instanceof ContainerServlet && (isContainerProvidedServlet(actualClass) || ((Context)getParent()).getPrivileged())) { ((ContainerServlet) servlet).setWrapper(this); } instanceSupport.fireInstanceEvent(InstanceEvent.BEFORE_INIT_EVENT, servlet); servlet.init(facade); instanceSupport.fireInstanceEvent(InstanceEvent.AFTER_INIT_EVENT, servlet); return servlet; }Through these components, Tomcat manages request handling, lifecycle control, and dynamic reloading of web applications.
Source: https://www.ibm.com/developerworks/cn/java/j-lo-tomcat1/index.html
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.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
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.
