java

Web Application Deployment Descriptors – “web.xml”

he “web.xml” contains the deployment descriptors. There are two sets of web.xml

:

  1. $CATALINA_HOME\conf\web.xml: applicable to all web application under Tomcat.
  2. WebContextRoot\WEB-INF\web.xml: applicable to the specific web context. It overrides the global setting, if any.

The complete specification for “web.xml” can be found in the “Java Servlet Specification” (@ http://java.sun.com/products/servlet), under “Deployment Descriptor”.

Suppose that you wish to deploy two servlets: TestServlet.class and QueryServlet.class (that have to be kept under your application’s “WebContextRoot\WEB-INF\classes” directory):

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" metadata-complete="true">

  <servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>TestServlet</servlet-class>
  </servlet>

  <servlet>
    <servlet-name>Query</servlet-name>
    <servlet-class>QueryServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/Echo</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>Query</servlet-name>
    <url-pattern>/Query</url-pattern>
  </servlet-mapping>
</web-app>

Explanation:

  • Each servlet requires two configuration elements. A <servlet> to specify the servlet-name and the servlet-class, and a <servlet-mapping> to specify the URL for that particular servlet-name. In the above example, the TestServlet can be referenced via URL http://host:port/context-path/Echo, and the QueryServlet via URL http://host:port/context-path/Query.
  • You have to list ALL the <servlet> elements first, followed by <servlet-mapping> elements.
Advertisements

One thought on “Web Application Deployment Descriptors – “web.xml”

  1. I personally contemplate the reasons why you labeled this blog post, “Web Application Deployment Descriptors – web.

    xml Stay Hungry Stay Foolish”. In any event I loved the post!
    Thanks a lot,Rosalind

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s