Mastering Servlets and Tomcat: Architecture, Components, and Request Flow
This guide explains what Servlets and the Servlet specification are, outlines Tomcat's architecture and core components, and walks through the detailed HTTP request processing flow, providing code examples and diagrams for Java backend developers.
This article is part of a Java interview series that is continuously updated.
What is a Servlet?
Servlet is a Java EE specification that extends Java for web services via a unified interface, implemented by containers such as Tomcat and Jetty. When an HTTP request arrives, the container creates an HttpServletRequest object, invokes lifecycle methods like init() and service(), and returns a response wrapped as HttpServletResponse.
What is the Servlet Specification?
Two JAR files: servlet-api.jar and jsp-api.jar (JSP is also a Servlet).
Two packages: javax.servlet and javax.servlet.http.
Defines interfaces such as Servlet, Filter, Listener, ServletRequest, and ServletResponse.
Why is Tomcat called a Web Container or Servlet Container?
Tomcat implements the Servlet specification and provides a container that holds our Servlets.
Tomcat Architecture Overview
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
</Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t \"%r\" %s %b" />
<Context docBase="F:/workspace/my-web-maven/target" path="/" reloadable="true" />
<Context docBase="F:/workspace/my-web-maven/target" path="/tian" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>The XML configuration mirrors the architectural diagram; classes such as Listener, Service, Host, and Engine correspond to Java implementations.
Tomcat Core Components
Server : Starts the Tomcat instance and provides lifecycle listeners and global naming resources.
Service : Groups one or more Connectors that share a Container.
Connector : Accepts client connections, creates request/response objects, and forwards them to the Engine.
Container : Manages Servlets and includes Engine, Host, Context, and Wrapper components.
Component Relationships
Tomcat Request Processing Flow
The request reaches port 8080 and is captured by the Coyote HTTP/1.1 Connector.
The Connector forwards the request to the Engine of its Service.
The Engine matches the request to a Host (default localhost).
The Host selects the appropriate Context based on the request path.
The Context finds the matching Servlet (e.g., JspServlet for *.jsp).
The Container creates HttpServletRequest and HttpServletResponse objects and invokes the Servlet's doGet or doPost.
The response propagates back through Context, Host, Engine, Connector, and finally to the client browser.
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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
