Skip to content

DispatcherServlet Request Processing

DispatcherServlet acts as the front controller in Spring MVC, responsible for handling incoming HTTP requests and orchestrating the entire request processing lifecycle. It delegates specific tasks—such as handler mapping, view resolution, and exception handling—to specialized strategy components.^[600-developer__java__spring__spring-note.md]

Initialization: Strategy Objects

Before processing requests, the DispatcherServlet initializes the strategy objects it uses to handle various aspects of the request lifecycle.^[600-developer__java__spring__spring-note.md] This initialization happens via the initStrategies method, which sets up the following components in order:^[600-developer__java__spring__spring-note.md]

  1. MultipartResolver (via initMultipartResolver)
  2. LocaleResolver (via initLocaleResolver)
  3. ThemeResolver (via initThemeResolver)
  4. HandlerMappings (via initHandlerMappings)
  5. HandlerAdapters (via initHandlerAdapters)
  6. HandlerExceptionResolvers (via initHandlerExceptionResolvers)
  7. RequestToViewNameTranslator (via initRequestToViewNameTranslator)
  8. ViewResolvers (via initViewResolvers)
  9. FlashMapManager (via initFlashMapManager)

Core Processing: doDispatch

The actual dispatching logic is contained within the doDispatch method, which processes all HTTP methods (GET, POST, etc.).^[600-developer__java__spring__spring-note.md] The workflow follows these key steps:

1. Multipart Request Check

The servlet first checks if the request is a multipart request (e.g., file upload) using checkMultipart(request).^[600-developer__java__spring__spring-note.md]

2. Handler Mapping

The servlet determines the handler for the current request by consulting the registered HandlerMappings in order.^[600-developer__java__spring__spring-note.md] This results in a HandlerExecutionChain, which contains the handler itself and any associated interceptors.^[600-developer__java__spring__spring-note.md]

3. Handler Adapter Resolution

Once a handler is mapped, the servlet looks up a HandlerAdapter that supports the specific handler class.^[600-developer__java__spring__spring-note.md] This adapter is responsible for actually invoking the handler.^[600-developer__java__spring__spring-note.md]

4. Pre-handle Interceptors

Before invoking the handler, the servlet applies pre-handle logic of the interceptors in the execution chain via mappedHandler.applyPreHandle.^[600-developer__java__spring__spring-note.md]

5. Handler Invocation

The HandlerAdapter is used to invoke the handler, which returns a ModelAndView object.^[600-developer__java__spring__spring-note.md]

6. Post-handle Interceptors

After the handler executes but before the view is rendered, the servlet applies post-handle logic via mappedHandler.applyPostHandle.^[600-developer__java__spring__spring-note.md]

7. Result Processing

The processDispatchResult method handles the final outcome, which includes rendering the view or resolving exceptions if they occurred during the dispatch.^[600-developer__java__spring__spring-note.md]

Exception Handling and Cleanup

If an exception or error occurs at any point during the dispatch attempt, it is caught and stored in a dispatchException variable.^[600-developer__java__spring__spring-note.md] This exception is then passed to the processDispatchResult method.^[600-developer__java__spring__spring-note.md]

Finally, regardless of success or failure, the servlet ensures resources are cleaned up. If a multipart request was parsed, cleanupMultipart is called to remove any temporary resources.^[600-developer__java__spring__spring-note.md]

Sources

  • 600-developer__java__spring__spring-note.md