How Spring MVC Simplifies Server‑Side Development: From Servlets to DispatcherServlet
This article walks through the evolution from traditional Servlets to Spring MVC's DispatcherServlet, explaining how each component—Servlet, BaseServlet, DispatcherServlet, HandlerMapping, HandlerInterceptor, HandlerExecutionChain, ModelAndView, ViewResolver, and View—contributes to a cleaner, more modular server‑side architecture.
1. The Former King – Servlet
Early Java web development relied heavily on Servlets, which handled request reception, processing, and response generation by wrapping raw HTTP data into HttpServletRequest and HttpServletResponse objects, allowing developers to focus on business logic.
2. Wanting to Go Further
Using plain Servlets becomes cumbersome as applications grow: each Servlet handles a single request, manual parameter extraction and validation are required, URLs are hard‑coded in web.xml, and Servlets are tightly coupled with view technologies.
3. Spring MVC – Two‑Level Controllers
Spring MVC introduces a front‑controller called DispatcherServlet , which acts as a “super Servlet”. Business‑logic classes are no longer Servlets but ordinary POJOs called Handlers (often implemented as Controllers ).
4. DispatcherServlet – Front‑Controller
The DispatcherServlet is the core of Spring MVC. It centralises request handling, performs automatic parameter binding, validation, URL matching, view resolution, and data rendering, effectively orchestrating the entire request‑processing flow.
5. HandlerMapper – Request Mapping Expert
Spring MVC replaces the static web.xml mapping with HandlerMapping implementations that associate request URLs with specific Handlers. Common strategies include SimpleUrlHandlerMapping, ControllerClassNameHandlerMapping, BeanNameUrlHandlerMapping, and the annotation‑driven @RequestMapping via DefaultAnnotationHandlerMapping.
6. HandlerInterceptor – Intercepting Requests
Before a request reaches a Handler, HandlerInterceptor s can pre‑process it (e.g., authentication, logging). Unlike Servlet Filter, an interceptor works at the Handler level, offering finer‑grained control.
7. Handler – The Secondary Controller
The Handler (or Controller) contains the actual business logic. Spring MVC isolates this logic from request handling, view rendering, and other cross‑cutting concerns.
8. HandlerExecutionChain – Bridging Handler and Interceptor
HandlerExecutionChain combines a Handler with its associated HandlerInterceptors, forming a chain of responsibility. The request passes through each interceptor; if all return true, the Handler is finally invoked.
9. ModelAndView – Decoupling Data and View
After processing, a Handler returns a ModelAndView object that holds the data model and the logical view name, separating business results from navigation concerns.
10. ViewResolver – Finding the View
The ViewResolver translates the logical view name into a concrete View implementation (e.g., JSP, FreeMarker, Velocity, XSLT, JasperReports). Various resolvers include BeanNameViewResolver, InternalResourceViewResolver, FreeMarkerViewResolver, etc.
11. View – Rendering the Data
The selected View renders the model data into the final response (HTML, JSON, PDF, etc.) and returns it to the DispatcherServlet, which writes it back to the client.
In summary, Spring MVC’s architecture—front‑controller, handler mappings, interceptors, execution chain, ModelAndView, view resolution, and rendering—provides a clean, modular approach to server‑side development, vastly simplifying the code compared to raw Servlets.
<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>
<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>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
