Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

How to Configure Dispatcher Servlet in web.xml File?

In a traditional Spring MVC application, the DispatcherServlet is the central servlet that dispatches requests to controllers. The web.xml is a deployment descriptor file for the web application, and it's where you configure the DispatcherServlet.

Here's a step-by-step guide to configure the DispatcherServlet in the web.xml:

  • Declare the DispatcherServlet: You need to declare the DispatcherServlet and specify its servlet name.
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

The <init-param> tag specifies the location of the Spring configuration file associated with the DispatcherServlet. This file typically contains the beans for controllers, view resolvers, and other web-related components. By convention, the file is usually named [servlet-name]-servlet.xml, so in our case, it's dispatcher-servlet.xml.

  • Map the DispatcherServlet to handle certain URL patterns: The <servlet-mapping> element specifies the URL patterns the DispatcherServlet will handle.
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

With the <url-pattern>/</url-pattern>, you're telling the DispatcherServlet to handle all requests to the application.

  • (Optional) ContextLoaderListener: If you have a root application context configuration, you can load it using the ContextLoaderListener. The root application context is separate from the DispatcherServlet context and is often used for beans that are shared across multiple servlets, like services, repositories, and data sources.
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

With this configuration, Spring will load the applicationContext.xml file on application startup, and its beans will be available to the entire application.

  • Final web.xml: After adding the above configurations, your web.xml might look something like this:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- DispatcherServlet Configuration -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Root Application Context Configuration (Optional) -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

With this configuration, your Spring MVC application will dispatch requests to controllers defined in dispatcher-servlet.xml, and other shared beans will be available from the applicationContext.xml file.

  1. Configuring DispatcherServlet in web.xml in Spring:

    • Description: This process involves configuring DispatcherServlet in the web.xml file, the traditional way of setting up Spring MVC in a Servlet-based application.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  2. Setting up DispatcherServlet in web.xml File:

    • Description: Set up DispatcherServlet in the web.xml file, specifying its servlet class and mapping to handle incoming requests.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  3. Spring MVC DispatcherServlet Configuration in web.xml:

    • Description: Configure Spring MVC's DispatcherServlet in the web.xml file, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  4. web.xml Configuration for DispatcherServlet in Spring:

    • Description: Use the web.xml file to configure DispatcherServlet in a Spring application, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  5. Defining DispatcherServlet in web.xml for Spring MVC:

    • Description: Define DispatcherServlet in the web.xml file for a Spring MVC application, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  6. Mapping DispatcherServlet in web.xml in a Spring Application:

    • Description: Map DispatcherServlet in the web.xml file for a Spring application, specifying its servlet class and the URL pattern it should handle.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  7. XML Configuration for DispatcherServlet in Spring web.xml:

    • Description: Utilize XML configuration in the web.xml file to set up DispatcherServlet for a Spring application, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  8. DispatcherServlet Initialization in web.xml: Spring MVC:

    • Description: Initialize DispatcherServlet in the web.xml file for a Spring MVC application, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  9. How to Specify DispatcherServlet in web.xml for Spring MVC:

    • Description: Specify DispatcherServlet in the web.xml file for a Spring MVC application, defining its servlet class and the URL pattern it should handle.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  10. web.xml Configuration for Front Controller in Spring:

    • Description: Configure the web.xml file for a Spring application, designating DispatcherServlet as the front controller with its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  11. Setting Servlet and Servlet-Mapping for DispatcherServlet in web.xml:

    • Description: Set up the Servlet and Servlet-Mapping in the web.xml file to configure DispatcherServlet with its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  12. Configuring DispatcherServlet in Traditional Spring MVC web.xml:

    • Description: Configure DispatcherServlet in the traditional Spring MVC way using the web.xml file, specifying its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  13. Configuring DispatcherServlet and ContextLoaderListener in web.xml:

    • Description: Configure both DispatcherServlet and ContextLoaderListener in the web.xml file, setting up the Spring MVC application with its servlet and context initialization.
    • Code:
      <web-app>
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>
          
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  14. Sample web.xml Configuration with DispatcherServlet for Spring MVC:

    • Description: A sample web.xml configuration demonstrating the setup of DispatcherServlet in a Spring MVC application, defining its servlet class and URL mapping.
    • Code:
      <web-app>
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>