Building a Web Application WAR File

WAR Files

A web application is packaged into single file known as a Web Archive (WAR) file.  The format and structure of this file is defined in the Servlet 2.2 specification (see Further Sources of Information). A WAR file uses the same format as the popular ZIP archive and as such can be compressed and expanded by a variety of tools.

Below are the typical contents of a WAR file:

/index.html
/images/logo.gif
/stock/stocklist.html
/WEB-INF/web.xml
/WEB-INF/lib/myapplication.jar
/WEB-INF/classes/com/mycompany/myservlet.class

A WAR file must contain a top level WEB-INF directory and inside it there must be a file called web.xml, known as the deployment descriptor.  Without it the Web Application cannot be deployed.  At the same level as web.xml there can exist two other directories lib and classes, these contain either jar files or java classes that are required by the servlets used in the web application.

The Deployment Descriptor web.xml

By far the most important file of a WAR file is web.xml. It is an XML file and its contents describe how the web application should be deployed within the web application container.

The deployment descriptor describes how the servlets that comprise the application should be configured, including what requested URI patterns map to which servlets, what initialisation parameters are to be passed to the servlets, the context in which they execute, security information and much more.

Refer to the Servlet 2.2 specification (see Further Sources of Information) and other online resources for full details.

Mapping Requests to Servlets

When a web application container receives a request for a URL it must decide how it is going to process the request.  This is done using a mapping table for URL patterns to servlets configured within the web application. There are four types of mapping:

The rules for defining which servlet is executed on a requested URL are as follows:

  1. The servlet container will try to match the exact path of the requested URL against the exact mapping definitions.

  2. The container will then try to recursively match the longest path prefix mapping. This process occurs by stepping down the path tree a directory at a time, using the '/' character as a path separator and determining if there is a match with the servlet.

  3. If the last node of the url-path contains an extension, the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the path after the last '.' character.

  4. If neither of the previous two rules match then the container will attempt to serve content appropriate for the resource requested. If a default servlet is defined then it will be used.

This process of mapping http requests to the relevant processing servlet is critical to the correct operation of a web application, should you modify the servlet mapping in web.xml deployment descriptor included in the sample Active Web web application then Active Web and its administration may fail to operate correctly.

Comment on this topic

Topic ID: 150011