Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

How to Configure Dispatcher Servlet in web.xml File?

The DispatcherServlet in a Spring MVC application serves as the front controller and handles incoming HTTP requests, delegating them to the appropriate controllers. In a traditional Spring MVC application (not Spring Boot), the DispatcherServlet is usually configured in the web.xml file.

Here's how you can configure the DispatcherServlet in web.xml:

  1. Setup Your web.xml File:

    If your project doesn't already have a web.xml, it should be located in the WEB-INF directory of your web application.

  2. Add DispatcherServlet Configuration:

    <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>
                <!-- This will look for a file named [servlet-name]-servlet.xml in the WEB-INF directory -->
                <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Servlet Mapping Configuration -->
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    
  3. Create dispatcher-servlet.xml Configuration:

    Based on the above configuration, Spring will look for a file named dispatcher-servlet.xml in the WEB-INF directory. This is where you define your Spring beans, controllers, view resolvers, etc.

    <?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"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context.xsd
                               http://www.springframework.org/schema/mvc
                               http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
    
        <!-- Package to scan for @Controller and other annotated components -->
        <context:component-scan base-package="com.example.myapp" />
    
        <!-- Define your view resolver, data sources, etc. here -->
    
    </beans>
    
  4. Define Controllers:

    With the above configuration, you can now define Spring MVC controllers in the package com.example.myapp (or any other package you specified in dispatcher-servlet.xml).

Remember, if you're using Spring Boot, the configuration is much simpler since it uses a convention-over-configuration paradigm and automatically configures the DispatcherServlet for you. The above approach is more for traditional Spring MVC applications.

  1. Configuring DispatcherServlet in web.xml example:

    • Description: An example illustrating the configuration of DispatcherServlet in the web.xml file for a Spring MVC application.
    • Code:
      <!-- web.xml -->
      <web-app>
          <servlet>
              <servlet-name>dispatcherServlet</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
      
          <servlet-mapping>
              <servlet-name>dispatcherServlet</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
  2. Setting up Dispatcher Servlet mapping in web.xml:

    • Description: Define the URL mapping for DispatcherServlet in the web.xml file to handle specific URL patterns.
    • Code:
      <!-- web.xml -->
      <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>/app/*</url-pattern>
      </servlet-mapping>
      
  3. Defining DispatcherServlet in web.xml for Spring MVC:

    • Description: Define DispatcherServlet in the web.xml file specifically for Spring MVC application handling.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>springMvcDispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>springMvcDispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
  4. Web.xml configuration for Dispatcher Servlet in Spring:

    • Description: Configuring DispatcherServlet in the web.xml file for a Spring application.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>myDispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>myDispatcherServlet</servlet-name>
          <url-pattern>/api/*</url-pattern>
      </servlet-mapping>
      
  5. Dispatcher Servlet initialization parameters in web.xml:

    • Description: Specify initialization parameters for DispatcherServlet in the web.xml file.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>dispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring-context.xml</param-value>
          </init-param>
      </servlet>
      
  6. Mapping URL patterns for Dispatcher Servlet in web.xml:

    • Description: Mapping specific URL patterns to be handled by DispatcherServlet in the web.xml file.
    • Code:
      <!-- web.xml -->
      <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>/api/*</url-pattern>
      </servlet-mapping>
      
  7. Defining Spring MVC servlet in web.xml file:

    • Description: Define the Spring MVC servlet in the web.xml file, specifying its servlet class.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>springMvcServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>springMvcServlet</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
  8. Configuring multiple Dispatcher Servlets in web.xml:

    • Description: Configure multiple DispatcherServlet instances in the web.xml file, each with its own servlet name and mapping.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>dispatcherServlet1</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>dispatcherServlet1</servlet-name>
          <url-pattern>/api/*</url-pattern>
      </servlet-mapping>
      
      <servlet>
          <servlet-name>dispatcherServlet2</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>dispatcherServlet2</servlet-name>
          <url-pattern>/admin/*</url-pattern>
      </servlet-mapping>
      
  9. Handling URL patterns with Dispatcher Servlet in web.xml:

    • Description: Define different URL patterns to be handled by DispatcherServlet based on the specified mappings in the web.xml file.
    • Code:
      <!-- web.xml -->
      <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>/api/*</url-pattern>
      </servlet-mapping>
      
      <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>/admin/*</url-pattern>
      </servlet-mapping>
      
  10. Configuring context parameters for Dispatcher Servlet in web.xml:

    • Description: Set up context parameters for DispatcherServlet in the web.xml file to provide additional configuration.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>dispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring-context.xml</param-value>
          </init-param>
      </servlet>
      
  11. Web.xml setup for Spring MVC application with Dispatcher Servlet:

    • Description: Configure the web.xml file for a Spring MVC application with DispatcherServlet and necessary mappings.
    • Code:
      <!-- web.xml -->
      <servlet>
          <servlet-name>springMvcDispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring-context.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>springMvcDispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
  12. Configuring servlet and servlet-mapping elements for Dispatcher Servlet: