The Java Servlet API is the standard Java API for handling HTTP requests and responses on a web server. A servlet is a Java class that a servlet container such as Apache Tomcat or Jetty creates and calls to process each request. Since Jakarta EE 9 the API lives in the jakarta.servlet package, and it was javax.servlet before that.
Most Java web frameworks, including Spring MVC and Jakarta Faces, are built on top of servlets, so understanding the API helps you understand how they work.
A servlet container (also called a web container) listens for HTTP connections, parses requests, maps URLs to servlets, manages their lifecycle and handles threads. Your servlet only has to read a HttpServletRequest and write a HttpServletResponse.
The API is now maintained as Jakarta Servlet. Version 6.0 is part of Jakarta EE 10, and version 6.1 is part of Jakarta EE 11. Tomcat 10.1 implements Servlet 6.0 and Tomcat 11 implements Servlet 6.1. Old applications written for Tomcat 9 or earlier use javax.servlet and must have their imports changed to jakarta.servlet to run on Tomcat 10 and later.
This servlet answers GET requests with a small JSON document. It uses the @WebServlet annotation, so no web.xml entry is needed.
After deploying the WAR file, a request to /hello?name=Ada returns the following.
For real services, build JSON with a library such as Jackson instead of concatenating strings.
The container controls the lifecycle, and each servlet class normally has one instance shared by all requests.
loadOnStartup is set.init(ServletConfig): called once so the servlet can set up resources.service(): called for every request. In HttpServlet it dispatches to doGet, doPost, doPut, doDelete and the other methods.destroy(): called once when the servlet is removed, to release resources.Because one instance serves many threads at once, never store per-request data in instance fields. Keep state in local variables, the request or the session.
| Type | Purpose |
|---|---|
Servlet | The base interface with the lifecycle methods |
HttpServlet | Base class with doGet, doPost and similar methods |
HttpServletRequest | Parameters, headers, cookies, body and session access |
HttpServletResponse | Status code, headers, cookies and the output body |
HttpSession | Per-user state across requests |
Filter | Intercepts requests and responses for logging, security or compression |
ServletContext | Application-wide configuration and resources |
AsyncContext | Asynchronous processing that frees the request thread |
This servlet reads a posted form field and tracks a visit counter in the session.
Session IDs are carried in a cookie. Configure the cookie as Secure and HttpOnly, and call request.changeSessionId() after login to prevent session fixation.
A Filter wraps the request chain, which is the right place for authentication checks, character encoding and request logging. Listeners such as ServletContextListener run code when the application starts and stops. For slow back-end calls, request.startAsync() releases the container thread while work continues elsewhere. Recent Tomcat and Jetty versions can also run request handling on Java virtual threads, which makes simple blocking code scale far better than before.
You rarely write many servlets by hand today. Spring MVC uses a single DispatcherServlet to route requests to controllers, and Jakarta REST implementations register their own servlet. Spring Boot embeds Tomcat, Jetty or Undertow, so you can run an application with java -jar without installing a container.
They are the same API under different package names. Oracle transferred Java EE to the Eclipse Foundation, and from Jakarta EE 9 onward the packages were renamed to jakarta.*.
No. The container normally creates one instance and calls it from many threads, so the class must be thread-safe.
Not for most settings. Annotations such as @WebServlet and @WebFilter replace most entries, though web.xml is still useful for some deployment-time configuration.
Tomcat is the most widely used, Jetty is lightweight and embeddable, and Undertow is the default in some stacks. All implement the same specification.
The Servlet API is the foundation of Java web development. A container manages the lifecycle, and your servlet handles requests through HttpServletRequest and HttpServletResponse. Use the jakarta.servlet package on current containers, keep servlets stateless and thread-safe, and rely on frameworks such as Spring for higher-level features built on the same model.