Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite
Next article icon

What is Dispatcher Servlet in Spring?

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. Now we are going to discuss one of the most important concepts in the Spring framework e.g Dispatcher Servlet.

Dispatcher Servlet

DispatcherServlet acts as the Front Controller for Spring-based web applications. So now what is Front Controller? So it is pretty simple. Any request is going to come into our website the front controller is going to stand in front and is going to accept all the requests and once the front controller accepts that request then this is the job of the front controller that it will make a decision that who is the right controller to handle that request. For example, refer to the below image. Suppose we have a website called student.com and the client is make a request to save student data by hitting the following URL student.com/save and its first come to the front controller and once the front controller accepts that request it is going to assign to the Controller_1 as this controller handle the request for /save operation. Then it is going to return back the response to the Client. 

Dispatcher Servlet

So now you might be thinking about how to create a front controller in a Spring MVC Application? But the good news is, the front controller is already created by the Spring Framework Developer, and the name of that particular controller is DispatcherServlet. You can use that front controller in your Spring MVC project. You really not required to create a front controller but you can reuse that front controller created by the Spring Framework Developer and they named it as DispatcherServlet. We can say

DispatcherServlet handles an incoming HttpRequest, delegates the request, and processes that request according to the configured HandlerAdapter interfaces that have been implemented within the Spring application along with accompanying annotations specifying handlers, controller endpoints, and response objects.

Setting Up Dispatcher Servlet

Now let's see how can we set up DispatcherServlet in our Spring MVC project.  

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 How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE? 

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: How to Create a Dynamic Web Project in Spring Tool Suite?

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. 

<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, hey servlet you have to handle all the requests coming to our website called student.com (for this example). So the way we are going to tell the above servlet is we can write something like this

<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>

So this does mean that the servlet "frontcontroller-dispatcher" is going to handle all the requests starting from student.com/anything, that may be student.com/save or student.com/get, anything. But it must start with student.com. So we are done with creating a Dispatcher Servlet. 

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 

<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. And if you are not writing this line of code then whenever the first request will come to your server starting from /student.com at that time only the DispatcherServlet will be initialized and we don't want it. We want the DispatcherServlet will be initialized during the time of the server startup. That's why we have written this line of code. The complete web.xml file is given below:

Example: File: web.xml 

XML
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns="http://xmlns.jcp.org/xml/ns/javaee"           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                               http://xmlns.jcp.org/xml/ns/javaee/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

So 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. So the 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="http://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.


 


 


Next Article
Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite

A

AmiyaRanjanRout
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring
Practice Tags :
  • Java

Similar Reads

    Spring MVC Tutorial
    In this tutorial, we'll cover the fundamentals of Spring MVC, including setting up your development environment, understanding the MVC architecture, handling requests and responses, managing forms, and integrating with databases. You'll learn how to create dynamic web pages, handle user input, and i
    7 min read
    Spring - MVC Framework
    The Spring MVC Framework follows the Model-View-Controller architectural design pattern, which works around the Front Controller, i.e., the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapp
    4 min read
    Spring MVC using Java Based Configuration
    Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles application integration. This enables the developer to create complex applications using plain Java classes. The model object can be passed between the view and the controller using map
    3 min read
    ViewResolver in Spring MVC
    Spring MVC is a powerful Web MVC Framework for building web applications. It provides a structured way to develop web applications by separating concerns into Model, View, and Controller. One of the key features of Spring MVC is the ViewResolver, which enables you to render models in the browser wit
    7 min read
    How to Create Your First Model in Spring MVC?
    Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
    6 min read
    How to Create Your First View in Spring MVC?
    Spring MVC is a powerful Web MVC Framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
    5 min read
    Spring MVC CRUD with Example
    In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows
    7 min read
    Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite
    Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain Java classes. The model object can be passed between the view and the controller
    5 min read
    What is Dispatcher Servlet in Spring?
    Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made
    6 min read
    Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite
    Eclipse is an Integrated Development Environment (IDE) used in computer programming. It includes a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development. Eclipse is written mostly in Java and its primary use is for dev
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences