Learn Basics of Sample web.xml in java

This post covers the basics of web.xml in the java web application.

Web.xml is a web deployment descriptor used for any web application written in java. Every web application

that is developed in java should have this XML file in Root-Folder/WEB-INF.Web.xml is defined as part java specification. so every vendor like tomcat, JBoss, or other app servers should have to implement in their web-based frameworks.web.xml contains all the JSP, servlet configuration, filter, listener configurations.

Mostly these days developer uses eclipse for web.xml modifications. This validates XML file against schema and gives a warning if any

web.xml configuration

We will see the different XML elements of the web deployment configuration.

xml version

<?xml version="1.0" encoding="UTF-8"?>

The above line is starting line for any XML file which contains the version and encoding type of XML.

Root element

<web-app xmlns="namespace" xmlns:xsi="schema instance"
          xsi:schemaLocation="schema location"

This is high-level root element which has xml namespace(xmlns) to identify the unique name for this xml.

As you know each XML should be validated against either DTD or schema. validation means for each tag, what are the sub-elements(display-name) and how many sub-elements i.e one or two

Version is servlet specification version for the web.xml.

Application metadata

<display-name>name of the application</display-name>
<description> short description </description>

Context parameter configuration

display-name represents the name of the web application. description configured to define the short description of your module

<param-name>conext initialization parameter</param-name>
<param-value>conext initialization value</param-value>

context-param is used to configure the key and value pair of values in web applications. These values are available through the web application. We can configure many context-param values for a web application.

Listener configuration

This listener entry is used to configure the listeners in the web application.

<listener-class>Listener java class</listener-class>

Servlet configuration

   <servlet-class>java servlet class with package</servlet-class>
   <param-name>init param</param-name>
     <param-value>init param value</param-value>

servlet entry configures the servlet defined for any web application with name and the servlet class.

load-on-startup is used to preload a servlet. Preloading means servlet will be loaded into web container when the application is started and calls the call-back methods of init methods.

we can specify the integer number which is the priority order given to load when multiple servlets are presented in a servlet container.

init-param is like context-param to define the key and values. These values are available to specific servlet only unlike context-param in the whole application.


servlet-mapping defines the mappings for the defined servlet name in the above step to a specific url pattern. servlet name will be called for the request url that ends with an HTML extension.

jsp configuration in web.xml

   <param-name>init param</param-name>
     <param-value>init parameter value</param-value>

JSP can also be declared like servlet except as seen in the above code and the mapping for the JSP page is as follows

<servlet-mapping>     <servlet-name>jsppage</servlet-name>

servlet-mapping defines the mappings for the defined JSP name in the above step to a specific url pattern. JSP name will be called for the request url that ends with HTML extension.

Filters name definition and mapping


Filters are called before servlets. Most of these are used for security purposes like login security implementation for checking authorizations and authentication Filter-mapping is used to define the filter class to a specific url pattern. These are the basic configurations in web.xml. These configurations are the same for tomcat or JBoss.

Sample web.xml file

  sample web.xml for reference
      <param-name>context parameter as key</param-name>
      <param-value>Context parameter value</param-value>


Important points

  1. The order all the elements should be as per Schema
  2. For every start tag, there must be an end tag.