Backend Development 17 min read

Understanding Spring MVC: From Servlets to DispatcherServlet and Request Handling

This article explains how Spring MVC evolved from basic Servlets to a sophisticated two‑level controller architecture, detailing the roles of DispatcherServlet, HandlerMapping, HandlerInterceptor, Handler, ModelAndView, ViewResolver, and View, while showing configuration examples and code snippets.

Top Architect
Top Architect
Top Architect
Understanding Spring MVC: From Servlets to DispatcherServlet and Request Handling

Spring MVC simplifies server‑side development by abstracting low‑level servlet handling and providing a two‑level controller model that separates request processing from business logic.

1. The Former King – Servlet

Early Java web development relied on raw Servlets, which required manual handling of request and response objects, URL mapping in web.xml , and explicit forwarding or redirection.

2. Wanting to Go Further

Using plain Servlets leads to problems such as a proliferation of servlet classes, repetitive parameter extraction, hard‑coded URL mappings, and tight coupling with view technologies.

3. Spring MVC – Two‑Level Controller

Spring introduces a front‑controller called DispatcherServlet that delegates requests to secondary controllers (Handlers). The secondary controller contains only business logic, while the front‑controller manages common concerns.

4. DispatcherServlet – Front‑Controller

The DispatcherServlet orchestrates request processing: it obtains request parameters, performs validation, selects the appropriate handler, and finally renders the response.

5. HandlerMapper – Request Mapping Expert

Spring uses HandlerMapping implementations to map URLs to Handlers. Common strategies include SimpleUrlHandlerMapping , ControllerClassNameHandlerMapping , BeanNameUrlHandlerMapping , and annotation‑based @RequestMapping via DefaultAnnotationHandlerMapping .

6. HandlerInterceptor – Intercepting Requests

Before a Handler executes, one or more HandlerInterceptor instances can inspect or reject the request (e.g., for authentication), providing finer‑grained control than servlet Filter .

7. Secondary Controller – Handler

The Handler (often a Spring @Controller ) contains the business logic and returns a logical view name.

8. HandlerExecutionChain – Bridging Handler and Interceptors

Spring wraps a Handler together with its interceptors in a HandlerExecutionChain , forming a chain that processes the request sequentially.

9. ModelAndView – Decoupling Data and View

Handlers populate a ModelAndView with data and a logical view name, leaving view rendering to separate components.

10. ViewResolver – Finding the View

Based on the logical view name, a ViewResolver selects an appropriate View implementation (e.g., InternalResourceViewResolver , FreeMarkerViewResolver , VelocityViewResolver , etc.).

11. View – Rendering Data

The chosen View renders the model data into the final response (HTML, JSON, etc.) and returns it to the DispatcherServlet , which sends it back to the client.

Below are typical configuration snippets used in a Spring MVC application.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    <servlet>
        <servlet-name>ShoppingServlet</servlet-name>
        <servlet-class>com.myTest.ShoppingServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>ShoppingServlet</servlet-name>
        <url-pattern>/shop/ShoppingServlet</url-pattern>
    </servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Spring MVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Initialize on startup -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

In summary, Spring MVC’s core lies in the DispatcherServlet , which coordinates request handling through HandlerMappings, interceptors, Handlers, ModelAndView, ViewResolvers, and Views, providing a clean separation of concerns and greatly improving development efficiency.

javaBackend DevelopmentSpring MVCDispatcherServletHandlerMapping
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.