DispatcherServlet:
The Heart of Spring Web MVC
- A “front controller”
- coordinates all request handling activities
- analogous to Struts ActionServlet / JSF FacesServlet
- Delegates to Web infrastructure beans
- Invokes user Web components
- Fully customizable
- interfaces for all infrastructure beans
- many extension points
In Spring MVC framework Dispatcher Servlet access Front Controller which handles all coming requests and queues for forwarding to the different controller. The front controller is a typical design pattern in the web applications development. In this case, a single servlet receives all requests and transfers them to all other components of the application. The task of the DispatcherServlet is sent a request to the specific Spring MVC controller.
DispatcherServlet is responsible for initializing the WebApplicationContext and it loads all configuration related to the web components like controllers, view resolver, interceptors etc., It will be loaded and initialized by calling init() method init() of DispatcherServlet will try to identify the Spring Configuration Document with naming conventions like “servlet_name-servlet.xml” then all beans can be identify.
DispatcherServlet in Spring MVC
public class DispatcherServlet extends HttpServlet { ApplicationContext ctx = null; public void init(ServletConfig cfg){ // 1. try to get the spring configuration document with default naming conventions String xml = "servlet_name" + "-servlet.xml"; //if it was found then creates the ApplicationContext object ctx = new XmlWebApplicationContext(xml); } ... }
- DispatcherServlet capture request URI
- and hand over to HandlerMapping.
- HandlerMapping search mapping bean with a method of the controller, where the controller returning the logical name(view).
- Then this logical name is sent to DispatcherServlet by HandlerMapping.
- Then DispatcherServlet tells ViewResolver to give the full location of view by appending prefix and suffix, then DispatcherServlet gives view to the client.
Spring MVC Related Posts
- Spring MVC Web Tutorial
- Spring MVC Interview Questions
- MVC Design Pattern
- Spring MVC DispatcherServlet
- Spring MVC WebApplicationContext and Root Application Context
- Spring MVC @Controller Annotation
- Spring MVC @RequestMapping Annotation
- Spring MVC @RequestParam Annotation
- Spring MVC ContextLoaderListener
- Spring MVC @RequestParam and @PathVariable annotations
- Spring MVC Hello World Example
- Spring MVC Exception Handling Example
- Spring MVC with Hibernate CRUD Example
- Spring MVC Tiles Plugin with Example
- Spring MVC Interceptor with example
- Spring MVC with MongoDB CRUD Example
- Spring MVC Internationalization & Localization with Example
the article was very good. Thanks for the valuable information , it helped me a lot and understood about DispatcherServlet.
But, I found few grammar mistakes in this article. Hope you correct those.
Thanks for your feedback. I resolved typos mistakes.