Monday 13 August 2012

What is web.xml (Deployment Descriptor) in Java?

In Java, all web applications have a web.xml file, also called deployment descriptor, in the \META-INF directory. The main purpose of this file is to provide information about the web application to deploy in the container.

It can be used to declare and configure servlets belonging to the application via descriptor elements. For example, tt helps setting filters, icons or define default welcome pages too.

Here is a web.xml example:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

  <display-name>My Application</display-name> 
  <description>My application description</description>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>somepackage.MyServletClass</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServletMapping</url-pattern>
  </servlet-mapping>

</web-app>
It defines a web application with one servlet called MyServlet. User requests to:

  http://www.mywebserver.com/MyAppPath/MyServletMapping/...

are sent to MyServlet. The MyAppPath application path is typically defined in the context.xml file. Both files serve different purpose and should not be confused with each other.

No comments:

Post a Comment