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"
          version="2.5">

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

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

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>
<listener-class>Listener java class</listener-class>
</listener>

Servlet configuration

<servlet>
 <servlet-name>name</servlet-name>
   <servlet-class>java servlet class with package</servlet-class>
      <load-on-startup>1</load-on-startup>
       <init-param>
   <param-name>init param</param-name>
     <param-value>init param value</param-value>
   </init-param>
  </servlet>

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>
     <servlet-name>name</servlet-name>
 <url-pattern>*.html</url-pattern>
  </servlet-mapping>

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

<servlet>
 <servlet-name>jsppage</servlet-name>
   <jsp-file>jspdemo.jsp</jsp-file>
   <init-param>
   <param-name>init param</param-name>
     <param-value>init parameter value</param-value>
   </init-param>
  </servlet>

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>
 <url-pattern>/myjsp</url-pattern>
  </servlet-mapping>

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

<filter>
  <filter-name>FilterName</filter-name>
    <filter-class>com.cloudhadoop.FilterClass</filter-class>
    </filter>
  <filter-mapping>
   <filter-name>FilterName</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-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

<web-app>
    <display-name>cloudhadoop</display-name>
    <description>
  sample web.xml for reference
    </description>
    <context-param>
      <param-name>context parameter as key</param-name>
      <param-value>Context parameter value</param-value>
    </context-param>
    <servlet>
      <servlet-name>servletName</servlet-name>
      <servlet-class>com.cloudhadoop.ServletClass</servlet-class>
      <init-param>
        <param-name>key</param-name>
        <param-value>value</param-value>
      </init-param>
    </servlet>
     <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>

</web-app>

Important points

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