A Java servlet is a server-side Java class that receives HTTP requests, runs your code, and writes a response. A servlet container such as Apache Tomcat, Jetty or Undertow creates the servlet, calls it for each matching request, and manages its lifecycle. Servlets are the foundation of Java web development, and frameworks like Spring MVC are built on top of them.
Servlets are often confused with applets. An applet ran inside a browser and has been removed from modern Java, while a servlet runs on the server and is still current. Since Jakarta EE 9 the API lives in the jakarta.servlet package instead of javax.servlet.
A browser sends an HTTP request to the container. The container maps the URL to a servlet, creates a request object and a response object, and calls the servlet's service method. In HttpServlet, service dispatches to doGet, doPost, doPut, doDelete and the other HTTP method handlers.
GET /hello?name=Ana./hello.doGet(request, response) on a worker thread.A single servlet instance serves many requests at the same time, so each request runs on its own thread against the same object.
The package depends on your platform. Jakarta EE 9 and later, Tomcat 10 and later, and Spring Boot 3 and later use jakarta.servlet. Older platforms such as Tomcat 9 use javax.servlet with the same class names.
The @WebServlet annotation registers the servlet without a web.xml file. Deploy the class in a WAR on a container (or embed Tomcat or Jetty) and request /hello?name=Ana:
If you return HTML that contains user input, escape it first. Writing raw parameters into a page opens the door to cross-site scripting.
The container controls the lifecycle through three methods.
| Method | When it runs | Typical use |
|---|---|---|
init(ServletConfig) | Once, after the instance is created | Load configuration, build shared resources |
service(request, response) | For every request | Dispatches to doGet, doPost and so on |
destroy() | Once, before the instance is removed | Release resources |
By default the container creates the servlet on the first request. Set loadOnStartup in @WebServlet to create it at deployment time instead.
Because one instance handles many requests, instance fields are shared between threads. Never store request-specific data, such as the current user, in a field.
request.getSession()) for per-user state, and keep session objects small.SingleThreadModel; it was removed from the API.A servlet is rarely alone. The Servlet API also provides:
jakarta.servlet.Filter), which wrap requests for logging, authentication, compression or CORS before they reach a servlet.request.startAsync()), which frees the container thread while a slow task finishes.In current servlet containers, ordinary blocking servlets also work well with virtual threads on Java 21 and later, if the container supports them. Tomcat, for example, can be configured to run requests on virtual threads.
| Technology | Role |
|---|---|
| Servlet | Low-level request and response handling |
| JSP (Jakarta Server Pages) | Templates that the container compiles into servlets |
| Spring MVC | Controllers on top of a single DispatcherServlet |
Most new applications use a framework, but every request still reaches a servlet underneath. Knowing the model helps you debug filters, sessions, encodings and threading issues.
A servlet runs on the server inside a container and produces responses. An applet was a program downloaded and run in the browser; applet support was deprecated in Java 9 and removed in later releases.
Not for most cases. @WebServlet, @WebFilter and @WebListener annotations replace it. You can still use web.xml to override annotations or configure global settings.
doGet handles safe, idempotent reads whose parameters appear in the URL. doPost handles requests that send data in the body and change state.
Yes. Spring Boot's web starter embeds a servlet container and registers Spring MVC's DispatcherServlet, so your controllers run inside the Servlet API.
A servlet is a Java class managed by a container that turns HTTP requests into responses. Extend HttpServlet, override the doXxx methods you need, and treat the instance as shared between threads. Use the jakarta.servlet package on current platforms, and remember that frameworks such as Spring MVC are built on this same model.