What is Dispatcher Servlet in Spring MVC?
Dispatcher Servlet is actually the heart of Spring MVC.Every single web request which is supposed to be processed by Spring MVC goes through DispatcherServlet.In general, its an implementation of Front Controller Pattern which provides a single point of entry in your application. It handles all incoming requests. It is also the bridge between Java and Spring.he DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling.
It is responsible for request handling by delegating requests to additional components of Spring MVC e.g. actual controller classes i.e. those which are annotated using @Controller or @RestController (in case of RESTful Web Services), Views, View Resolvers, handler mappers, etc.
Though the job of actual request mapping is done by @RequestMapping annotation, it's actually the DispatcherServlet which delegates request to the corresponding controller.
In the case of RESTful web services, it is also responsible for finding the correct message converter to convert the response into the format client is expecting like JSON, XML, or TEXT.
For example, if a client is expecting JSON then it will use the MappingJacksonHttpMessageConverter or MappingJackson2HttpMessageConverter (depending upon whether Jackson 1 or Jackson 2 is available in Classpath) to convert the response returned by convert into a JSON string.
==DispatcherServlet Request Processing===
Essentially, a DispatcherServlet handles an incoming HttpRequest, delegates the request, and processes that request according to the configured HandlerAdapter interfaces that have been implemented within the Spring application along with accompanying annotations specifying handlers, controller endpoints, and response objects.
Let's get more in-depth about how a DispatcherServlet processes a component:
**The WebApplicationContext associated to a DispatcherServlet under the key DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE is searched for and made available to all of the elements of the process
**The DispatcherServlet finds all implementations of the HandlerAdapter interface configured for your dispatcher using getHandler() – each found and configured implementation handles the request via handle() through the remainder of the process
**The LocaleResolver is optionally bound to the request to enable elements in the process to resolve the locale
**The ThemeResolver is optionally bound to the request to let elements, such as views, determine which theme to use
**if a MultipartResolver is specified, the request is inspected for MultipartFiles – any found are wrapped in a MultipartHttpServletRequest for further processing
**HandlerExceptionResolver implementations declared in the WebApplicationContext picks up exceptions that are thrown during processing of the request.