Open In App

What is Dispatcher Servlet in Spring?

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

DispatcherServlet is the Front Controller in a Spring web application. It acts as the entry point for all incoming HTTP requests. When a user makes a request (e.g., student.com/save), the DispatcherServlet receives it first, then decides which controller should handle it (e.g., Controller_1 for /save). After the controller processes the request, the response is sent back to the user.

Dispatcher Servlet

DispatcherServlet handles incoming HTTP requests and delegates them to the appropriate components. It works with HandlerAdapter interfaces configured in the Spring application to process the request. It uses annotations like @Controller, @RequestMapping, etc., to identify handler methods (controller endpoints) and prepares the appropriate response objects based on the controller's output.

Setting Up Dispatcher Servlet

Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS in your local machine.

Step 1: Create a Dynamic Web Project in your STS IDE. You may refer to this article to create a Dynamic Web Project in STS.

Step 2: Download the spring JARs file from this link and go to the src > main > webapp > WEB-INF > lib folder and past these JAR files. 

Step 3: Refer to this article Configuration of Apache Tomcat Server and configure the tomcat server with your application. Now we are ready to go.

Step 4: Now go to the src > main > webapp > WEB-INF > web.xml file and here we have to configure our front controller inside a <servlet>...</servlet> tag something like this. 

XML
<servlet>
      <!-- Provide a Servlet Name -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    <!-- Provide a fully qualified path to the DispatcherServlet class -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

Now let's tell this servlet, you have to handle all the requests coming to our website called student.com (for this example). To tell servlet this. we can write something like this.

XML
<servlet-mapping>

      <!-- Provide a Servlet Name that you want to map -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    
    <!-- Provide a url pattern -->
    <url-pattern>/student.com/*</url-pattern>
    
</servlet-mapping>

When Dispatcher Servlet will be Initialized: The Dispatcher Servlet will be Initialized once we deploy the created dynamic web application inside the tomcat server. So before deploying it let's add the following line inside the web.xml file 

<load-on-startup>1</load-on-startup>

So now the modified code for the servlet is 

XML
<servlet>
      <!-- Provide a Servlet Name -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    
    <!-- Provide a fully qualified path to the DispatcherServlet class -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
    <load-on-startup>1</load-on-startup>
    
</servlet>

Why this line "<load-on-startup>1</load-on-startup>"?

This will make sure that whenever your server will get started the DispatcherServlet will get initialized. If you don't write this line of code then whenever the first request will come to your server starting from /student.com, that time only the DispatcherServlet will be initialized.

Example: File: web.xml 

XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html" 
         xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html 
                             http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  
  <display-name>myfirst-mvc-project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  <absolute-ordering/>
  
  <servlet>
      <!-- Provide a Servlet Name -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    <!-- Provide a fully qualified path to the DispatcherServlet class -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <!-- Provide a Servlet Name that you want to map -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    <!-- Provide a url pattern -->
    <url-pattern>/student.com/*</url-pattern>
  </servlet-mapping>
  
</web-app>

Step 5: Now go to the  src > main > webapp > WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file. And the name of the file must be in this format

YourServletName-servlet.xml

For example, for this project, the name of the file must be 

frontcontroller-dispatcher-servlet.xml

Either you can create a Spring Configuration File or you can just create a simple XML file add the below lines of code inside that file. Code for the frontcontroller-dispatcher-servlet.xml is given below. 

Example: frontcontroller-dispatcher-servlet.xml 

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context/"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/
        https://www.springframework.org/schema/context/spring-context.xsd">

</beans>

 The Project file structure is as follows:

project structure


 Now run your application. To run the application refer to the below image.

Now in the Console, you can see our DispatcherServlet has successfully initialized and also initialization completed without any exceptions or errors.


Similar Reads