SPRING CONCEPTS

Servlet

Spring Concepts / Servlet

A Servlet is a Java server component that handles requests through a Servlet container.

Understand the idea

HttpServlet provides method-based dispatch: GET normally reaches doGet and POST reaches doPost. Tomcat creates and initializes the instance, invokes it for requests, and destroys it when removed. Multiple requests may use the same instance, so visitor state must not live in mutable Servlet fields.

A concrete example

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req,
                         HttpServletResponse res) { ... }
}

In the application

Manual 00 introduces HelloServlet, then TodoServlet for session-backed CRUD before the Spring controllers.

Servlet Java runs on the server. The browser receives its HTTP response, not a Java class to execute.

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly