1. The DispatcherServlet is an implementation of Front Controller and a Java Servlet component.
  2. DispatcherServlet is the front controller class that receives all incoming HTTP client requests for the Spring Web MVC application.
  3. DispatcherServlet is Servlet implemented as a subtype of HttpServlet just like any other servlet.
  4. DispatcherServlet is also required to be configured in our web application like any other servlet,that is ,into Web application deployment descriptor(web.xml).

The following code snippet shows the declaration of DispatcherServlet in web.xml.

<!--Declaring DispatcherServlet -->
<servlet>
	<servlet-name>ds</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

We can configure the initialization parameters to alter the behaviour of the DispatcherServlet with respect to locating the Spring Beans XML configuration files and initializing the application context.

<!--Declaring DispatcherServlet -->
<servlet>
	<servlet-name>ds</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>
		/WEB-INF/configs/applicationBeans.xml
		/WEB-INF/configs/applicationControllers.xml
	  </param-value>
	</init-param>
	<load-on-startup>0</load-on-startup>
</servlet>

The above configuration code declares the DispatcherServlet that specifies the servlet to use applicationContext.xml and applicationControllers.xml documents to initialize WebApplicationContext.

After declaring the DispatcherServlet we need to configure mappings for it.DispatcherServlet mapping should be generic.Thus in general we use path or extension mappings.

<servlet-mapping>
	<servlet-name>ds</servlet-name>
	<url-pattern>*.htm</url-pattern>
</servlet-mapping>

As per the mapping shown in the above code ,all the requests for this context whose servletPath ends with .htm extension are dispatched by the web container to the DispatcherServlet.

Note:
Spring Web MVC framework doesn’t force us to use specific extensions or patterns in URLs.

DispatcherServlet initialization stage

DispatcherServlet

  1. While initializing the DispatcherServlet ,it creats WebApplicationContext implementation class instance,either user configured custom context class ot the default XmlWebApplicationContext.
  2. WebApplicationContext is responsible to locate the Spring Beans XML configuration file,then read,validate the configurations and load the details into configuration objects.
  3. The DispatcherServlet uses the WebApplicationContext for accessing various framework objects that are used to execute the workflow for handling the request.

The following are the special framework objects that are initialized using the WebApplicationContext.

  1. MultipartResolver
  2. LocaleResolver
  3. ThemeResolver
  4. HandlerMapping
  5. HandlerAdapter
  6. HandlerExceptionResolver
  7. RequestToViewNameTranslator
  8. ViewResolver