17. Deployment, Tomcat & Spring MVC Progression

DispatcherServlet, Spring MVC & Production Pitfalls

Master the architectural progression from raw Servlets to Spring MVC DispatcherServlet, complete request sequence flows, production pitfalls, and self-assessments.

DispatcherServlet, Spring MVC & Production Pitfalls

Spring MVC does not replace the Servlet model—it builds a powerful high-level framework on top of Servlets using a single master entry point called the DispatcherServlet.


1. Traditional Servlet Scaling Bottlenecks

In traditional Java web applications, every distinct URL path required a separate custom Servlet class:

TRADITIONAL SERVLET SCALING BOTTLENECK:
GET  /users    ──► UserServlet (Manual parameter parsing, JSON serialization, DB calls)
POST /orders   ──► OrderServlet (Manual parameter parsing, JSON serialization, DB calls)
PUT  /products ──► ProductServlet (Manual parameter parsing, JSON serialization, DB calls)

As application size scales to hundreds of endpoints, managing dozens of independent Servlet mappings in web.xml causes massive boilerplate, duplicated parameter parsing, and fragmented exception handling.


2. Spring MVC & DispatcherServlet Front Controller Pattern

Spring MVC resolves this bottleneck by registering one single master Servlet—the DispatcherServlet—with Tomcat.

HTTP GET /users Route request to DispatcherServlet.service() Lookup handler method for "GET /users" Return UserController.getUsers() Invoke getUsers() (Auto-binds parameters) Call business service logic Return List<User> Return Java Objects (Jackson converts to JSON) Write HTTP Response Stream HTTP 200 OK (JSON Payload) Browser Client Tomcat Container DispatcherServlet (Central Entry Point) HandlerMapping UserController (@RestController) UserService

3. The Complete 6-Stage Architectural Progression

1. Raw Java Sockets (java.net.ServerSocket)
   └── Low-level TCP byte streams, manual HTTP parsing
2. Servlet Container (Apache Tomcat)
   └── Managed network listening, multithreading, URL routing
3. Servlet API (jakarta.servlet.http.HttpServlet)
   └── Request/Response object abstractions (init, service, destroy)
4. Front Controller Pattern (DispatcherServlet)
   └── Single entry-point Servlet routing to annotated controllers
5. Spring MVC Framework
   └── Automatic parameter binding, JSON conversion (Jackson), exception handling
6. Spring Boot
   └── Auto-configuration, opinionated defaults, embedded Tomcat JAR deployment

4. Production Pitfall Matrix

Production PitfallCommon MisconceptionCorrect Engineering Reality
1. Servlet Port Ownership"My HelloServlet listens on port 8080"Tomcat listens on port 8080; Tomcat dispatches requests to Servlets
2. Per-Request Instantiation"Tomcat creates a new Servlet object for every request"Tomcat creates one Servlet instance reused across all request threads
3. Shared Instance StateStoring request data in Servlet instance fieldsStore state ONLY in method-local variables on the thread stack
4. Bundling Container APIsOmitting <scope>provided</scope> in MavenMark Servlet API as provided to avoid classloader collisions
5. Overriding service()Replacing service() directly in HttpServletOverride doGet(), doPost(), doPut(), doDelete() instead
6. Replacing Container Model"Spring Boot eliminated Servlets"Spring Boot uses Servlets under the hood via embedded Tomcat

❓ Interactive Self-Assessment

Knowledge Check

What is the central role of DispatcherServlet in Spring MVC?

Servlet Execution Pipeline Debugging Challenge

A developer claims: "My custom OrderServlet opens port 8080 using new ServerSocket(8080) and creates a new OrderServlet instance every time a client sends an HTTP GET request."

Identify the 2 major architectural errors in the developer's statement and write down the correct execution sequence.

On this page