Unveiling Tomcat’s Architecture: From Servlet Spec to Core Components
This article explains the servlet specification, core servlet APIs, and how Tomcat implements them, detailing the roles of ServletConfig, ServletContext, ServletRequest, and ServletResponse, and walks through Tomcat’s internal architecture, including server.xml configuration, component hierarchy, and the interaction of Server, Service, Engine, Host, and Context.
Tomcat Architecture Design
1. Servlet Specification
1.1 What Servlets Do
Servlet is a JavaEE specification that extends Java for web services, defining interfaces such as Servlet, HttpRequest, HttpResponse, and Filter, which are implemented by containers like Tomcat and Jetty.
The specification does not provide an implementation; a container receives an HTTP request, parses it, creates a servlet instance, calls its init method, wraps request data in HttpServletRequest, invokes the servlet’s service method, and finally uses HttpServletResponse to send the response back to the client.
1.2 Core Servlet APIs
Key servlet APIs that are essential for understanding Tomcat:
ServletConfig : obtain servlet initialization parameters and the ServletContext object.
ServletContext : share data across the entire web application.
ServletRequest : encapsulate HTTP request information.
ServletResponse : encapsulate HTTP response information.
ServletConfig is created by the container during servlet initialization and passed to the servlet via the init() method.
Obtain initialization information.
Obtain the ServletContext object.
ServletContext is a single object per web application, created when Tomcat starts and destroyed when it stops, allowing data sharing among servlets.
public class ServletTwoImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// 1. Parameter passing
ServletContext servletContext = this.getServletContext();
String value = String.valueOf(servletContext.getAttribute("name"));
System.out.println("value=" + value);
// 2. Get init parameter
String userName = servletContext.getInitParameter("user-name");
System.out.println("userName=" + userName);
// 3. Get application info
String servletContextName = servletContext.getServletContextName();
System.out.println("servletContextName=" + servletContextName);
// 4. Get paths
String pathOne = servletContext.getRealPath("/");
String pathTwo = servletContext.getRealPath("/WEB-INF/");
System.out.println("pathOne=" + pathOne + ";pathTwo=" + pathTwo);
response.getWriter().print("执行:doGet; value:" + value);
}
}1.3 ServletRequest
HttpServletRequest extends ServletRequest and encapsulates request data. Core functions: obtain request message, network connection information, and request attribute data.
1.4 ServletResponse
HttpServletResponse extends ServletResponse and encapsulates response data. Core functions: set response headers, send status code, set response body, and perform redirects.
2. Tomcat Design
Tomcat implements the servlet specification by providing a container that receives HTTP requests, parses them, creates servlet instances, and invokes their service methods to generate responses.
2.1 What Is Tomcat
Tomcat is a servlet container that implements part of the J2EE/Jakarta EE specifications, serving as a server for Java web applications.
2.2 Tomcat Architecture Structure
The server.xml file defines Tomcat’s components; each XML element corresponds to a specific component.
Official documentation: https://tomcat.apache.org/tomcat-8.5-doc/config/server.html
<Server port="8005" shutdown="SHUTDOWN">
<Service name="Catalina">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
<Connector executor="tomcatThreadPool" 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"/>
<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"/>
</Host>
</Engine>
</Service>
</Server>2.3 Component Classification
Top‑level elements:
Server: the root element of the configuration file.
Service: groups an Engine and its Connectors.
Connector: the interface through which external clients send requests to a Service.
Container: processes requests from Connectors; Engine, Host, and Context are containers arranged in a parent‑child hierarchy.
Engine: handles all requests.
Host: handles requests for a specific virtual host.
Context: handles requests for a specific web application.
The components work together as follows: a Server contains one or more Services; each Service contains one Engine and one or more Connectors; the Engine contains Hosts; each Host contains Contexts.
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 High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
