DispatcherServlet processes requests as follows:

  • The WebApplicationContext is looked up and bound in the request as an attribute that can be used by the controller and other process elements. By default, it is associated with the DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE key.

  • The locale resolver is bound to the request to allow process elements to determine the locale to use when processing the request (view rendering, data preparation, and so on). If you don't need to recognize your locale, then you don't need a locale recognizer.

  • A theme resolver is bound to the request so that elements such as views can determine which theme to use. If themes are not used, then you can ignore it.

  • If a multi-part file recognizer is specified, the request will be checked for the presence of multi-part elements. If multipart elements are found, then the request is wrapped in a MultipartHttpServletRequest for further processing by other process elements.

  • A search for a suitable handler occurs. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) is started to prepare the model for rendering. Additionally, for annotated controllers, the response can be rendered (within a HandlerAdapter) instead of returning a view.

  • If a model is returned, the view is rendered. If the model is not returned (perhaps because the preprocessor or postprocessor intercepted the request, probably for security reasons), the view is not created because the request may have already been executed.

The HandlerExceptionResolver beans declared in the WebApplicationContext are used to recognize exceptions that are thrown when processing a request. These exception handlers allow you to customize exception handling logic.

To provide HTTP caching, handlers can use the CheckNotModified methods from WebRequest, as well as additional options for annotated controllers.

You can customize individual DispatcherServlet instances by adding servlet initialization parameters (init-param elements) to the servlet declaration in the web.xml file. The following table lists the supported parameters:

Table 1. DispatcherServlet initialization parameters
Parameter Explanation

contextClass

A class that implements ConfigurableWebApplicationContext, which will be instantiated and locally configured by this servlet. The default is XmlWebApplicationContext.

contextConfigLocation

A string that is passed to the context instance (specified by contextClass) to indicate where to look for contexts. A string potentially consists of multiple lines (using a comma delimiter) to support multiple contexts. In the case of multiple context locations with beans that are defined twice, the last location takes precedence.

namespace

Namespace WebApplicationContext. Default [servlet-name]-servlet.

throwExceptionIfNoHandlerFound

Controls whether a NoHandlerFoundException should be thrown if no handler was found for the request. The exception can then be caught using HandlerExceptionResolver (for example, using a controller method with the @ExceptionHandler annotation) and processed like any other.

By default, this parameter is set to false, in which case DispatcherServlet sets the message status to 404 (NOT_FOUND) without throwing an exception.

Note that if default servlet processing is also configured, unresolved requests are always forwarded to the default servlet and no 404 message is generated.