Servlets
The component that handles an HTTP request, and the contract the container imposes on you.
Java Servlets
A servlet is the only thing in a Jakarta EE application that actually turns an HTTP request into a response. Everything else — JSP, JSTL, filters, entire frameworks like Spring MVC — is built on top of it. When you write a Spring @RestController, it ends up in a servlet named DispatcherServlet. Understanding this class means understanding the foundation of the entire Java web.
The problem the servlet solves is concurrency. A hundred visitors hit your server at the same time. Do you need to create a hundred objects? A hundred threads? Who destroys them? The specification's answer is counterintuitive and has major consequences: the container creates a single instance of your servlet and lets all threads run through it simultaneously. This decision, made on your behalf, explains half of the production bugs in Java web applications: an instance field in a servlet is state shared among all users.
This lesson follows a request from end to end: how the container chooses which servlet to call, when it instantiates and destroys the object, how to read parameters and headers, how to decide between a redirect and a forward, and how to recognize a visitor from one request to the next when HTTP has no memory.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion