Updated Jun 22, 2026

The Servlet API From Zero

Underneath every Java web framework you'll ever use — Spring MVC, Jakarta EE's JAX-RS, even Quarkus on the JVM — there's one ancient, stable foundation: the Servlet API. Spring's DispatcherServlet is, as the name says, a servlet. JAX-RS runs on servlets. Every "middleware" you've heard of is a servlet filter underneath. This is the bedrock the whole Java web world is built on, and almost nobody learns it directly anymore — which is exactly why the frameworks feel like magic.

This is a roots guide. You'll rarely write raw servlets in a real job (the frameworks exist for good reasons), but understanding them turns a dozen framework concepts from magic into mechanism: routing, middleware, the request lifecycle, why your controllers must be thread-safe, how sessions work. We build the mental model bare-metal first — an HTTP request arriving, a container handing it to your code, a response going back — and then you'll recognize that exact shape inside every framework you touch.

📝 This assumes Java (classes, interfaces, inheritance) and a basic grasp of HTTP. If HTTP is fuzzy, read HTTP, Explained first. This guide is the deepest "kill the magic" root under Spring and Jakarta EE — most valuable after you've used a framework and want to see what's beneath it.

How to read this

Read in order — it builds from a single bare servlet up to the front-controller pattern that frameworks generalize. Short and foundational. Phases carry difficulty badges.

The phases

  1. What a Servlet Is 🟢 — the foundational unit of Java web: an object that handles HTTP requests, and the container that runs it.
  2. The Servlet Container & Lifecycle 🟡 — init/service/destroy, one instance serving many threads, and why that demands thread-safety.
  3. Handling Requests with HttpServlet 🟢 — doGet/doPost, reading the request, writing the response, by hand.
  4. Mapping & the Front-Controller Pattern 🟡 — URL mapping, and the one-servlet-routes-everything pattern that is DispatcherServlet's secret.
  5. Filters & the Chain 🟡 — intercepting requests before/after your servlet — the root of all "middleware."
  6. Sessions & State 🟡 — HttpSession, cookies, and how stateful behavior is built on a stateless protocol.
  7. From Servlets to Frameworks 🟢 — see the servlet inside Spring MVC, JAX-RS, and middleware; where to go next.

Once you've seen the Servlet API bare, "a framework" reads as "conveniences over a servlet, a front controller, and a filter chain." The magic was always this.


Phase 1: What a Servlet Is →