Open In App

WebApplicationContext in Spring MVC

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

WebApplicationContext in Spring is a web-aware version of ApplicationContext that has access to ServletContext. Each DispatcherServlet in a web application has its own WebApplicationContext, configured using a specific *-servlet.xml file. This means a single web app can have multiple DispatcherServlets, each with its own context and config file.

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

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.

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: Configuring Dispatcher Servlet: Go to the src > main > webapp > WEB-INF > web.xml file and  the complete code for web.xml file is given below:

XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html" 
         xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html 
                             http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html/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>

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. 

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://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>


Step 5: Create Your Spring MVC Controller

Now, let's create some controllers. Go to the src/main/java and create a new controllers package (For ex. com.student.controllers) as per your choice. And inside that create a Java class and name the class as DemoController. Now how to tell the Spring that this is our controller class. So the way we are going to tell the Spring is by marking it with a @Controller annotation.

Java
@Controller
public class DemoController {
    
}

Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.

Now let's create a simple method inside the Controller class and use @RequestMapping and @ResponseBody annotation before the method something like this.

Java
@ResponseBody
@RequestMapping("/hello")
public String helloWorld() {
    return "Hello World!";
}

Now let's understand both annotations. 

@RequestMapping("/hello") means when someone visits student.com/hello, the linked method will run. If the method returns a message like "Hello World!", it won’t show in the browser by default. To display it, we need to use @ResponseBody, which tells Spring to write the returned message directly to the HTTP response.

Java
package com.student.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {
    
    @ResponseBody
    @RequestMapping("/hello")
    public String helloWorld() {
        return "Hello World!";
    }

}


Step 6: Add the below line inside the frontcontroller-dispatcher-servlet.xml file

XML
<context:component-scan base-package="com.student.controllers"></context:component-scan>

So the complete code for the frontcontroller-dispatcher-servlet.xml is given below. 

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://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">

    <context:component-scan base-package="com.student.controllers"></context:component-scan>

</beans>


Step 7: Run Your Spring MVC Controller

To run your Spring MVC Application right-click on your project > Run As > Run on Server and run your application as shown in the below image. 

After that use the following URL to run your controller as shown in the below image. All other details are mentioned in the image. 

http://localhost:8080/myfirst-mvc-project/student.com/hello


Similar Reads