What is Dispatcher Servlet in Spring?

Kevin picture Kevin · May 5, 2010 · Viewed 197.2k times · Source

In this image (which I got from here), HTTP request sends something to Dispatcher Servlet.

enter image description here

My Question is what does Dispatcher Servlet do?

Is it something like getting the information thrown from the web page and throwing it to the controller?

Answer

Affe picture Affe · May 5, 2010

The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location.

I might have

  • a file /WEB-INF/jsp/pages/Home.jsp
  • and a method on a class

    @RequestMapping(value="/pages/Home.html")
    private ModelMap buildHome() {
        return somestuff;
    }
    

The Dispatcher servlet is the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document.

How it accomplishes this varies widely with configuration and Spring version.

There's also no reason the end result has to be web pages. It can do the same thing to locate RMI end points, handle SOAP requests, anything that can come into a servlet.