How Dispatcher servlet works in Spring MVC

Role of Spring MVC in Dispatcherservlet


As I said, DispatcherServlet wears many hats in Spring. It acts as a front controller and provides a single entry point for the application. It then uses handler mappings and handler adapters to map a request to the Spring MVC controllers. It uses @Controller and @RequestMapping annotation for that purpose.
 
 Once the request is processed by the Spring MVC controller, it returns a logical view name instead of the view. Though, you can even configure Controler's handler methods to not return any View name by declaring return type as void. You can even use @ResponseBody annotation in the case of REST to directly write the output to the HTTP response body. See REST with Spring course by Eugen to learn more about developing RESTful web services using Spring MVC.
 
 When DispatherServlet receives view name, it consults the ViewResolver to find the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They try to resolve the logical view name into a Physical resource e.g. a JSP page or a FreeMaker or Velocity template.
 
 The ViewResolver are invoked in order, if first in the chain not able to resolve the view then it returns null and next ViewResolver in the chain is consults. Once the right view is found, DispatcherServlet forwards the request along with Model data to the View for rendering e.g. a JSP page.
 
 
 By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix and suffix to convert a logical view name e.g. "home" to /WEB-INF/home.jsp. The View interface also has getContentType() method, which returns content type the view produces (JstlView has text/html). This is usually the default content type for requests handled by the dispatcher servlet in Spring.
 
 Here is a nice diagram which explains how DispatcherServlet works internally in Spring MVC
In short, DispatcherServlet is used following things in Spring MVC
 - receives all request as Front Controller and provides a single entry point to the application
 - mapping requests to correct Spring MVC controller
 - Consulting ViewResolvers to find correct View
 - forwarding request to chosen View for rendering
 - returning the response to the client
 - creates web-context to initialize the web specific beans e.g. controllers, view resolvers and handler mapping
 
 That's all about what is the use of DispatcherServlet in Spring framework. It's is one of the key components of Spring MVC which is used to receive all incoming request and forward them to right controllers for actual processing. It finds the right controllers by using handler mappings e.g. SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which check if the bean name is the same as view name and the bean implements the View interface.
 
 If you are using annotations then it can also use @Controller and @RequestMapping annotations to find the right controller to process a particular request. Once the request is processed by the controller it returns a logical view name to DispatcherServlet.
 
 The DispatcherServlet then consults ViewResolver and LocalResolvers to find the right View to render the output. Once the correct View is chosen, it forwards the request to the View for rendering the response.

**Related blog:**

[Java concurrency method example](http://candidjava.com/tutorial/java-concurrency-and-executors-framework-example/)