The Jakarta Servlet API defines a server-side request and response model. Apache
Tomcat implements that API and supplies the Catalina servlet container, HTTP and
AJP connectors, session management, deployment, and operational tooling.
Tomcat 11 implements Jakarta Servlet 6.1 and requires Java 17; older applications
that import javax.servlet.* need migration to the jakarta.servlet.* namespace
or an older compatible container.
Web Application Layout #
A deployable web application can be an expanded directory or a WAR archive:
orders.war
|-- index.html
`-- WEB-INF
|-- web.xml
|-- classes/com/example/OrdersServlet.class
`-- lib/orders-domain.jarResources below WEB-INF/ are not directly addressable by clients.
WEB-INF/classes/ contains application classes and resources, while
WEB-INF/lib/ contains dependency JARs. WEB-INF/web.xml is the deployment
descriptor. Servlet 3.0 and later also support annotation-based registration and
programmatic registration through ServletContainerInitializer, so web.xml
can be absent unless deployment requires descriptor metadata or ordering.
Mapping and Request Dispatch #
A servlet mapping selects a servlet from a URL pattern. Exact mappings win over
the longest path mapping, followed by extension mappings and the default /
mapping. A filter can wrap requests and responses before or after the target,
and listeners observe context, session, and request lifecycle events.
Dispatches have distinct DispatcherType values:
REQUESTrepresents the original container dispatch.FORWARDtransfers processing without a client round trip.INCLUDEinserts another resource’s output.ERRORinvokes configured error handling.ASYNCcontinues an asynchronous request.
request.getRequestDispatcher("/WEB-INF/views/order.jsp")
.forward(request, response);A redirect instead sends a 3xx response, after which the client starts a new request. During a normal dispatch, the connector parses the request, Catalina selects the virtual host and web context, the filter chain runs, and one servlet instance handles the call. Containers call a servlet concurrently; mutable instance fields therefore require synchronization or, preferably, elimination.
Sessions are identified by a cookie or URL rewriting. Session attributes may be
accessed by concurrent requests and must not be treated as thread-confined.
Async processing releases the request thread, but the application must complete
the AsyncContext and preserve any required security or logging context.
Tomcat’s Container Model #
org.apache.catalina.startup.Bootstrap starts a standalone installation.
catalina.home points to the shared Tomcat binaries, while catalina.base
points to an instance’s configuration, logs, temporary files, work directory,
and deployed applications. Multiple instances can thus share one installation.
Catalina composes these elements:
- A
Serverowns the Tomcat instance. - A
Serviceassociates one or moreConnectors with oneEngine. - An
Engineroutes requests to aHost. - A
Hostcontains one or more web applicationContexts. - A
Wrapperrepresents an individual servlet.
Connectors own protocol processing and worker threads. Blocking servlet work occupies those threads, so connector limits, downstream timeouts, and application executor limits must form one capacity plan.
Class Loaders and Shutdown #
Tomcat separates bootstrap, system, common, and per-web-application class
loaders. A web application sees its own WEB-INF/classes and WEB-INF/lib but
not another application’s classes. Tomcat’s web loader uses local-first lookup
for application classes, with mandatory delegation for Java, Jakarta Servlet,
and container implementation APIs. Duplicate API JARs in WEB-INF/lib do not
override container APIs and can cause linkage failures.
For graceful shutdown, stop accepting new traffic, allow in-flight work to
finish within a deadline, and then stop the Server. Servlet context listeners
and managed resources receive shutdown callbacks, but application-created
executors and threads still need explicit termination. Session persistence
depends on the configured manager and must not be assumed across a restart.
Comments
Read or comment on GitHub (sign-in required to comment)