Backend Development 5 min read

Deep Dive into Spring MVC Request Processing Flow

This article walks through the Spring MVC request handling pipeline step by step, explaining how DispatcherServlet sets attributes, resolves handlers, invokes handler adapters, runs pre‑ and post‑interceptors, creates ModelAndView, resolves the view and finally renders the response, with code examples from the original framework.

Java Captain
Java Captain
Java Captain
Deep Dive into Spring MVC Request Processing Flow

Spring MVC Process Overview

Spring MVC is a frequent interview topic in China; this article provides a concise yet thorough walkthrough of its source code to help readers understand the underlying mechanics.

Diagram

Code Location

The entry point is com.interface21.web.servlet.DispatcherServlet#doService , the original Spring 1.0 implementation that remains conceptually simple.

1. Set Request Attributes

// 1. Set attributes
// Make web application context available
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());

// Make locale resolver available
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);

// Make theme resolver available
request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);

2. Resolve Handler Execution Chain

// 2. Find handler and return execution chain
HandlerExecutionChain mappedHandler = getHandler(request);

3. Obtain HandlerAdapter

// This will throw an exception if no adapter is found
// 3. Return the handler's adapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

The HandlerAdapter interface is intended for framework developers, not application programmers; it allows custom workflow handling.

4. Execute Pre‑Handle Interceptors

// 4. Loop through handler's pre interceptors
for (int i = 0; i < mappedHandler.getInterceptors().length; i++) {
    HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
    // pre interceptor
    if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) {
        return;
    }
}

5. Invoke the Actual Handler

// 5. Execute the real handler and obtain ModelAndView (handler may be a proxy, possibly with AOP)
ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler());

6. Execute Post‑Handle Interceptors

// 6. Loop through handler's post interceptors
for (int i = mappedHandler.getInterceptors().length - 1; i >= 0; i--) {
    HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
    // post interceptor
    interceptor.postHandle(request, response, mappedHandler.getHandler());
}

7. Resolve the View

View view = null;
if (mv.isReference()) {
    // We need to resolve this view name
    // 7. Resolve View instance from ModelAndView
    view = this.viewResolver.resolveViewName(mv.getViewName(), locale);
}

8. Render the View

// 8. Render the view and return the response
view.render(mv.getModel(), request, response);

In summary, the DispatcherServlet orchestrates attribute setup, handler lookup, adapter delegation, interceptor chains, handler execution, view resolution, and final rendering.

PS: If you found this share useful, feel free to like or forward it.

(End)

JavaSpring MVCWeb FrameworkDispatcherServletHandlerAdapter
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.