Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Java servlet is a web component that runs under a servlet container. The container maps incoming requests, supplies request and response objects, manages the servlet’s lifecycle, and returns the response; the servlet runs the application-specific logic. Understanding that division explains both how a request becomes a response and why servlet code must account for concurrent requests.

What a servlet container does

A servlet is not a standalone program that listens for web traffic by itself. A servlet container provides the runtime in which servlet components operate. A web server or application server receives a request and either passes it to the container or integrates with the container directly. The container uses servlet configuration and mappings to select a component, invokes it with request and response objects, and handles the response through its server integration.

The Jakarta Servlet Specification 6.1 defines a servlet as “a Jakarta technology-based web component, managed by a container, that generates dynamic content.” In short, the container supplies the web machinery; the servlet applies the application’s rules.

How an HTTP request becomes a response

  1. A client sends a request. A browser or other client sends an HTTP request to a web server or application server.
  2. The container selects a servlet. The container receives the request, directly or through its host server, and matches it to a servlet using its mappings and configuration.
  3. The container supplies API objects. For HTTP handling, the servlet receives an HttpServletRequest and an HttpServletResponse.
  4. HTTP method handling is dispatched. An HTTP servlet commonly extends HttpServlet. Its service handling dispatches requests to method-specific handlers such as doGet or doPost.
  5. The servlet runs application logic. Code reads relevant request data, performs the required work, sets the response status and headers, and writes the response body.
  6. The container returns the response. The container completes response processing and sends it through its integration with the server.

For example, a servlet handling a GET request might read a query parameter from the request, use it to select application data, set a content type and status on the response, and write output. The container handles the surrounding web runtime rather than requiring each servlet to implement its own network server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens during the servlet lifecycle

The container controls the servlet’s standard lifecycle. It does not create a new servlet instance for every request.

  1. Load and instantiate: The container loads the servlet class and creates an instance. It may do this during startup or wait until the servlet is needed.
  2. Initialize: Before handling requests, the container calls init. Use it for one-time setup and for reading servlet configuration, not for work that belongs to each request.
  3. Handle requests: The container calls service with request and response objects. For an HttpServlet, HTTP method handling is dispatched to methods such as doGet and doPost.
  4. Leave service: When taking the servlet out of service, the container calls destroy so the servlet can release resources or perform cleanup.

Why servlet code must be thread-safe

In the default, non-distributed deployment model, a servlet declaration is served by one instance. The specification allows the container to handle concurrent requests through that instance, so multiple request-handling threads can access its fields at the same time.

Avoid storing request-specific or user-specific mutable data in servlet instance fields. Keep such data in local variables or appropriate request-scoped data instead. Shared mutable fields can cause one request to interfere with another. The specification strongly recommends against synchronizing the servlet’s service method because doing so can impose performance costs.

How request data and response writing work

HttpServletRequest exposes request information, including parameters and other request data. Do not assume a particular parameter will always be available: whether parameters are parsed depends on the request type and when the container processes it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

With HttpServletResponse, set the status and headers before the response is committed, then write the body using the response writer or output stream. Once committed, changes to headers are ignored. This makes response ordering important: decide status and headers before output commits the response.

Choosing between Tomcat 10.1 and Tomcat 11

Match the container to the Servlet API version, Java runtime, and namespaces used by the application and its dependencies. The following compatibility details are documented by Apache Tomcat:

Container Servlet specification Minimum Java version Typical servlet namespace
Tomcat 10.1 Servlet 6.0 Java 11 jakarta.servlet
Tomcat 11 Servlet 6.1 Java 17 jakarta.servlet

Jakarta Servlet 6.1 is the current standard covered here; its final specification was released on March 28, 2024, and sets Java SE 17 as the minimum platform for compliant containers. Tomcat 11 implements Servlet 6.1. Tomcat 10.1 implements Servlet 6.0.

These are Tomcat Servlet capabilities, not a claim that Tomcat is a full Jakarta EE application server. If an application depends on other APIs or libraries, check their compatibility separately before choosing a runtime.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What changed from javax.servlet to jakarta.servlet

Older Java EE servlet applications commonly import classes from javax.servlet. Tomcat 10 and later use jakarta.* packages instead, including jakarta.servlet. Apache documents this as a breaking change: moving an older application can require recompilation and code changes, not just replacing the server.

Apache provides a migration tool, but the namespace change can affect application code and related APIs. Before moving to Tomcat 10 or 11, check the imports, dependencies, and Java baseline of the application. Older examples can still explain servlet concepts, but their imports and dependency coordinates may not match a Jakarta-based project.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.