The inner working of DispatcherServlet

The main program of Spring MVC is DispatcherServlet. Most Spring MVC webapp’s web.xml is as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns=""

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->



Above example shows that every url is handled by DispatcherServlet. DispatcherServlet itself is JEE servlet. Without understanding inner working, we can not correlate several Spring MVC components (ex. controller, view, interceptor)

The following is inner working of the program (based on Spring 4.3.11)


  • Main Spring context is initialized by ContextLoaderListener. Usually “contextConfigLocation” param indicate main bean config file. (which is inside <context-param>)
  • DispatcherServlet context is initialized by “contextConfigLocation” param (which is child of <init-param>). This context is child of main Spring context
  • Therefore, beans in root-context.xml can not reference beans in servlet-context.xml.

Main sub component

  • Main logic is implemented at DispatcherServlet.doDispatch()
  • HandlerExecutionChain contains handler and interceptors. Handler is usually controller class (where @Controller is defined)
  • HandlerAdapter is the execution engine of the handler
  • HandlerAdapter itself can render view (return is null) or delegate rendering to View (return is ModelAndView)
  • ModelAndView is the container for the result data and view

Main processing sequence

  1. HandlerExecutionChain is created
  2. Based on HandlerExecutionChain, a specific type of HandlerAdapter is created
  3. Based on HandlerExecutionChain, each interceptor’s preHandle() is executed. (org.springframework.web.servlet.HandlerInterceptor)
  4. Generated handler adapter is executed. This is the point where controller is executed. And the result type is ModelAndView
  5. Based on HandlerExecutionChain, each interceptor’s postHandle() is executed
  6. If ModelAndView is not null, matching view’s render() is executed. This is the point where response is shown. Here, a specific view is resolved by ViewResolver (More on ViewResolver)



One thought on “The inner working of DispatcherServlet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.