How a Java Servlet request flows through the container, filters, and servlet
Jakarta Servlet is the server API that connects HTTP requests and responses to Java web application code. The container selects a mapped resource, runs the applicable filter chain, and invokes an HttpServlet, which reads the request and builds the response. This guide uses Jakarta Servlet 6.1 (Java SE 17 or later) to explain that flow, servlet lifecycle, concurrent requests, and the boundary of asynchronous processing.
Jakarta Servlet is the server API that connects HTTP requests and responses to Java web application code. The container selects a mapped resource, runs the applicable filter chain, and invokes an HttpServlet, which reads the request and builds the response. This guide uses Jakarta Servlet 6.1 (Java SE 17 or later) to explain that flow, servlet lifecycle, concurrent requests, and the boundary of asynchronous processing.
The container routes an HTTP request to a servlet
A servlet container is part of a web or application server. It handles network requests and manages servlet lifecycles. Based on URL mappings, it selects a resource and supplies HttpServletRequest and HttpServletResponse objects for an HTTP request. The container is not the servlet itself; socket, thread, and protocol internals vary by product.
Client
→ web server / servlet container
→ URL mapping and filter chain
→ HttpServlet.service()
→ doGet() / doPost() and similar methods
→ response unwinds through filters in reverse order
→ container flushes and sends the responseFilters handle shared concerns before and after a servlet, such as authentication, logging, or header transformation. Calling FilterChain.doFilter(request, response) advances to the next filter or target resource. If a filter does not call it, that filter must finish the response. A filter can perform response-side work after the downstream call returns. Filter order comes from application configuration.
The servlet is initialized, then can handle many requests
The container loads and instantiates a servlet, then calls init() before routing requests to it. It calls service() for each routed request; HttpServlet dispatches by HTTP method to doGet(), doPost(), doPut(), and other methods. When removing an instance, the container calls destroy() so it can release resources. The container may initialize a servlet at startup or defer it until first use.
In a typical non-distributed web application, a servlet declaration has one instance, but the container may invoke that instance’s service() concurrently on multiple threads. Do not store request-specific users, carts, or response data in instance fields: concurrent requests could see one another’s values. Keep request values in method-local variables. If state must be shared, design its concurrency safety and lifetime explicitly. Synchronizing all of service() can serialize requests and reduce throughput.
@WebServlet("/greeting")
public class GreetingServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
if (name == null || name.isBlank()) {
name = "world";
}
response.setContentType("text/plain; charset=UTF-8");
response.getWriter().println("Hello, " + name + "!");
}
}This minimal example reads a request parameter and writes plain text with the Jakarta Servlet 6.1 API. Since name varies per request, it is a local variable. The code has not been compiled or deployed to a container. Add validation and context-appropriate output encoding; HTML output requires HTML escaping.
The response can close when the service method returns
A servlet writes status, headers, and body through HttpServletResponse. A container may buffer output; the response can become committed when the buffer is flushed or flushBuffer() is called. After commitment, changing the status or headers and resetting the buffer is generally no longer possible. For a synchronous request, the normal flow is that the container flushes remaining output and closes the response after service() returns. sendError() and sendRedirect() also affect when the response ends.
Do not keep request or response objects beyond the synchronous method scope. A container may recycle them. Only when async processing has started does the response remain valid until AsyncContext.complete() or container-managed completion.
Async processing returns the request thread while work waits
Waiting for a slow service or queue can tie up a request thread. Servlet async processing starts the work and returns the request thread to the container; after the work finishes, it can write a response or use AsyncContext.dispatch() to route back to a resource in the container. The servlet and every applicable filter in the request chain must support async. asyncSupported defaults to false on @WebServlet and @WebFilter.
@WebServlet(value = "/async-greeting", asyncSupported = true)
public class AsyncGreetingServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) {
AsyncContext context = request.startAsync();
context.setTimeout(5_000);
context.start(() -> {
try {
String result = slowService.fetch();
context.getRequest().setAttribute("result", result);
context.dispatch("/render-result");
} catch (Exception ex) {
context.getRequest().setAttribute("failure", ex);
context.dispatch("/error");
}
});
}
}This sketch stores a result on the existing request and dispatches to a container resource for rendering. slowService, /render-result, and /error are placeholders that the application must provide. Production code needs an AsyncListener for timeout/error handling, cancellation and resource cleanup, and a deliberate failure response. Avoid simultaneous access to request/response objects from the async work and original request thread. Async processing frees a waiting request thread; it does not remove the work or guarantee higher throughput.
Troubleshoot by checking the boundary that failed
**A filter never reaches the servlet:** check URL or servlet-name mappings, filter order, and whether `doFilter()` is called. If the chain is intentionally stopped, make sure the filter writes the response.
**Data leaks between requests:** look for request-specific state in servlet instance fields and unsafe concurrent mutation of shared objects.
**Response header changes have no effect:** check whether the response was committed by a buffer flush or body write.
**An async request ends too early or hangs:** verify async support on every servlet and filter in the chain, the `startAsync()` call, a terminal `complete()` or `dispatch()` path, and timeout/listener handling.
**Resources remain during shutdown:** release them within their ownership scope using `destroy()` or `AsyncListener.onComplete()`. The time given to in-flight requests during shutdown is bounded by container settings.
Scope and sources
[Jakarta Servlet 6.1 specification](https://jakarta.ee/specifications/servlet/6.1/jakarta-servlet-spec-6.1.html) — container, request flow, lifecycle, concurrency, response, and filter rules
[Jakarta Servlet API 6.1 package docs](https://jakarta.ee/specifications/servlet/6.1/apidocs/jakarta.servlet/jakarta/servlet/package-summary.html) — contract between API types and the container runtime
[Jakarta EE Tutorial: Servlets](https://jakarta.ee/learn/docs/jakartaee-tutorial/current/web/servlets/servlets.html) — servlet basics and async processing examples
[Tomcat 11 request process flow](https://tomcat.apache.org/tomcat-11.0-doc/architecture/requestProcess.html) — one versioned example of a container’s endpoint-to-servlet path
Scope: Jakarta Servlet 6.1, with Java SE 17 as the minimum platform. Tomcat 11 is one implementation example; container internals differ by product. Official sources were read on 2026-11-20. The examples are illustrative and were not compiled, run in a container, load-tested, or tested for failure handling.