Spring MVC Web.xml & Spring-Servlet.xml – Code Example

The article presents information around two key configuration files and code samples that one could pickup, put in their web application folder and get up and running.

What & Why Web.xml?

Web.xml is a deployment descriptor file. Simply speaking, see web.xml as a file used to describe classes, resources and configurations which is used by web server to serve the requests. As the request reaches to the web server, the server uses web.xml to map the URL of the request to the code that would handle the request.

While working with Spring MVC, the server, in turn, delegates the request to DispatcherServlet which retrieves appropriate controller that would be used to handle the request and serve the request using the view. The view (jsp file) is decided by DispatcherServlet using the ViewResolver.

Thus, web.xml would need to have configuration related with DispatcherServlet. Following is the sample code:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app 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"
    id="WebApp_ID" version="3.1">

        <!-- Spring Configuration -->

Why Spring-Servlet.xml?

The spring related configuration, as per Spring MVC convention, is stored in the file named using -servlet.xml where ServletName is the name of DispatcherServlet defined in web.xml file. In above web.xml file, the servlet name of DispatcherServlet is “spring” and thus, the name “spring-servlet.xml”. The spring-servlet file consists of information related with components such as controller and POJOs along with hibernate related configuration which are used to serve the request.

Following is the simplistic code for spring-servlet.xml. Pay attention to codeĀ .

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

<context:component-scan base-package="com.vitalflux.helloworld" />

<!-- Configuration defining views files -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
        <property name="suffix">


Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data Science and Machine Learning / Deep Learning. In addition, I am also passionate about various different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia etc and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data etc. I would love to connect with you on Linkedin.
Posted in Java. Tagged with , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.