Servlets
A servlet is a Java class the container calls for an HTTP request. You override doGet or doPost, read the request, write the response. Spring MVC sits on this model even when you never write a servlet yourself.
Detailed Explanation
Java's server-side web story started with the Servlet API. A container (Tomcat, Jetty, Undertow) listens on a port, parses HTTP, and invokes service on a servlet mapped to a URL. HttpServlet splits that into doGet, doPost, and the other verbs. HttpServletRequest holds headers, query, body, session. HttpServletResponse holds status, headers, and the output stream.
Servlets are typically singletons. Many threads call doGet at once. Instance fields are shared state and need synchronization or, better, no mutable fields. Request state lives on the request object.
Jakarta EE renamed javax.servlet to jakarta.servlet. Spring Boot 3 uses Jakarta. Filters wrap the servlet. Listeners hook into start/destroy and session events. Async servlets (startAsync) let you hold a request without occupying a container thread the whole time; virtual threads change that calculus.
You reach for a raw servlet when you are writing a tiny endpoint or learning the stack. You reach for Spring MVC when you have routing, validation, and JSON. You still need servlet knowledge to debug filters, sessions, multipart, and request.getInputStream() consumed twice.
The cost of storing user data on the servlet instance is a race. The cost of writing to the response after it is committed is an error. The cost of blocking a servlet thread on a downstream call is a thread-pool stall; use async or virtual threads.
web.xml mappings still exist; annotations (@WebServlet) are common. In Spring, DispatcherServlet is the one servlet that fronts all controllers.
When something in Spring "does not see" a header or a parameter, you are still in servlet land: a filter consumed the body, a forward changed the path, or the request was wrapped. Knowing getParameter vs the raw query string, and when the container parses multipart, saves hours. The Servlet spec is older than Boot. It is still the request object you debug.
Code Examples
import java.io.IOException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setStatus(200);
resp.setContentType("text/plain; charset=UTF-8");
resp.getWriter().write("hello");
}
}import jakarta.servlet.http.HttpServletRequest;
public class Query {
static String name(HttpServletRequest req) {
String n = req.getParameter("name");
return n == null ? "guest" : n;
}
}Common Gotchas
Mutable servlet fields are shared across requests
keep state on the request or in a store.
Reading the body twice fails
filters that read JSON must wrap the request.
`javax.servlet` vs `jakarta.servlet`
Spring Boot 3 needs Jakarta.
Writing after `sendRedirect` or `flush` commits the response
Writing after `sendRedirect` or `flush` commits the response