Skip to content
Related Articles

Related Articles

Spring – Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 10 Mar, 2022

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 developing Java applications, but it may also be used to develop applications in other programming languages via plug-ins, including C, C++, C#, Groovy, JavaScript, etc. Similarly, Spring Tool Suite (STS) is a java IDE tailored for developing Spring-based enterprise applications. It is easier, faster, and more convenient. And most importantly it is based on Eclipse IDE

What is Dispatcher Servlet in Spring?

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.

In this article What is Dispatcher Servlet in Spring? we have created our DispatcherServlet manually. But let’s take benefit of Spring Tool Suite IDE and let’s create DispatcherServlet with a shortcut method. 

Step by Step Implementation:

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? Make sure that you have selected Target runtime when creating the Dynamic Web Project as show in the below image. 

Step 2: Right-click on the project name > New > Servlet as show in the below image.

Step 3: In the Create Servlet popup check out the “Use an existing Servlet class or JSP” box and click on the Browse button as show in the below image.

Step 4: In the next screen you have to select the Servlet and search for Dispatcher Servlet and select it and click on the OK button as show in the below image.

Step 5: Now After selected click on the Next button. Refer to the below image if you ar stuck somewhere. 

Step 6: In the next screen chose your desired Dispatcher Servlet Name and also Edit your URL mappings as show in the below image. Anld click on the Finish button. You are done.

In the web.xml file you can see your Dispatcher Servlet has been created. 

Tip: Below two lines are not required so you can delete them

<description></description>
<display-name>demo-frontcontroller</display-name>

And here is the complete web.xml file

Example

XML




<?xml version="1.0" encoding="UTF-8"?>
         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>demo-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>
  <servlet>
    <description></description>
    <display-name>demo-frontcontroller</display-name>
    <servlet-name>demo-frontcontroller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo-frontcontroller</servlet-name>
    <url-pattern>/demo.com/*</url-pattern>
  </servlet-mapping>
</web-app>

 
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!