Open In App

How to Configure Dispatcher Servlet in web.xml File?

Improve
Improve
Like Article
Like
Save
Share
Report

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 making a request to save student data by hitting the following URL student.com/save, and its first comes 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.

Here we are going to see how can we configure Dispatcher Servlet in the web.xml File. 

In Spring, the /WEB-INF/web.xml file is the Web Application Deployment Descriptor of the application. This file is an XML document that defines everything about your application that a server needs to know (except the context path, which is assigned by the Application Deployer and Administrator when the application is deployed), servlets, and other components like filters or listeners, initialization parameters, container-managed security constraints, resources, welcome pages, etc. 

Tip: Do refer to the below image to know where the web.xml file is located when one can get how to create a Dynamic Web Project in Spring Tool Suite.

web.xml File

 

Step 1: Configure Dispatcher Servlet in web.xml File

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 gfg.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>/gfg.com/*</url-pattern>
    
</servlet-mapping>

So this does mean that the servlet “frontcontroller-dispatcher” is going to handle all the requests starting from gfg.com/anything, that may be gfg.com/save or gfg.com/get, anything. But it must start with gfg.com. So we are done with configuring a Dispatcher Servlet. And 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>

And the above piece of code makes 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 /gfg.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.

File: web.xml

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>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>
 
<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>/gfg.com/*</url-pattern>
</servlet-mapping>
 
</web-app>


Step 2: Testing Our Application

So now we want to see whether our Spring Dispatcher Servlet has been successfully initialized or not? And to check it go to the  src > main > webapp > WEB-INF and create an XML file. Actually, this is a Spring Configuration file like the beans.xml file. And the name of the file must be in this format

YourServletName-servlet.xml  

For example, 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 and add the below lines of code inside that file. 

File: frontcontroller-dispatcher-servlet.xml 

Now,

Step 3: Run the application

In order 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.

 



Last Updated : 02 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads