What is the DispatcherServlet in Spring and its uses?

DispatcherServlet:
The Heart of Spring Web MVC


What is the DispatcherServlet in Spring

In Spring MVC framework Dispatcher Servlet access Front Controller which handles all coming requests and queues for forward to the different controller. 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 to all other components of the application. The task of the DispatcherServlet is send request to the specific Spring MVC controller.

DispatcherServlet is responsible for initialize 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.

Popular Tutorials

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);
    }
    ...
}

What is used of DispatcherServlet in Spring


  1. DispatcherServlet capture request URI 
  2. and hand over to HandlerMapping.
  3. HandlerMapping search mapping bean with method of controller, where controller returning logical name(view).
  4. Then this logical name is send to DispatcherServlet by HandlerMapping.
  5. Then DispatcherServlet tell ViewResolver to give full location of view by appending prefix and suffix, then DispatcherServlet give view to the client.

Spring MVC Related Posts
  1. Spring MVC Web Tutorial
  2. Spring MVC Interview Questions
  3. MVC Design Pattern
  4. Spring MVC DispatcherServlet
  5. Spring MVC WebApplicationContext and Root Application Context
  6. Spring MVC @Controller Annotation
  7. Spring MVC @RequestMapping Annotation
  8. Spring MVC @RequestParam Annotation
  9. Spring MVC ContextLoaderListener
  10. Spring MVC @RequestParam and @PathVariable annotations
  11. Spring MVC InternalResourceViewResolver
  12. Spring MVC Hello World Example
  13. Spring MVC Exception Handling Example
  14. Spring MVC with Hibernate CRUD Example
  15. Spring MVC Tiles Plugin with Example
  16. Spring MVC Interceptor with example
  17. Spring MVC with MongoDB CRUD Example
  18. Spring MVC Internationalization & Localization with Example

Labels: , , ,