Previous | Top | Next
Background Layout Descriptor Integration

# 3. DEPLOYMENT ORGANIZATION

## 3.1 Background

Before describing how to organize your source code directories, it is useful to examine the runtime organization of a web application. Prior to the Servlet API Specification, version 2.2, there was little consistency between server platforms. However, servers that conform to the 2.2 specification are required to accept a Web Application Archive in a standard format, which is discussed further below.

A web application is defined as a hierarchy of directories and files in a standard layout. Such a hierarchy can be accessed in its "unpacked" form, where each directory and file exists in the filesystem separately, or in a "packed" form known as a Web ARchive, or WAR file. The former format is more useful during development, while the latter is used when you distribute your application to be installed.

The top-level directory of your web application hierarchy is also the document root of your application. Here, you will place the HTML files and JSP pages that comprise your application's user interface. When the system administrator deploys your application into a particular server, he or she assigns a context path to your application. Thus, if the system administrator assigns your application to the context path `/catalog`, then a request URI referring to `/catalog/index.html` will retrieve the `index.html` file from your document root.

## 3.2 Standard Directory Layout

To facilitate creation of a Web Application Archive file in the required format, it is convenient to arrange the "executable" version of your web application (that is, the files that Tomcat actually uses when executing your app) in the same organization as required by the WAR format itself. To do this, you will end up with the following contents in your application's "document root" directory:

- *.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript and stylesheet files) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files.
- WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you. This file is discussed in more detail in the following subsection.
- WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for your application, including both servlet and non-servlet classes, that are not combined into JAR files. If your classes are organized into Java packages, you must reflect this in the directory hierarchy under `WEB-INF/classes/`. For example, a Java class named `com.mycompany.mypackage.MyServlet` would need to be stored in a file named `WEB-INF/classes/com/mycompany/mypackage/MyServlet.class`.
- WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

When you install an application into Tomcat (or any other 2.2-compatible server), the classes in the `WEB-INF/classes/` directory, as well as all classes in JAR files found in the `WEB-INF/lib/` directory, are added to the class path for your particular web application. Thus, if you include all of the required library classes in one of these places (be sure to check licenses for redistribution rights for any third party libraries you utilize), you will simplify the installation of your web application -- no adjustment to the system class path will be necessary.

Much of this information was extracted from Chapter 9 of the Servlet API Specification, version 2.2, which you should consult for more details.

## 3.3 Web Application Deployment Descriptor

As mentioned above, the `WEB-INF/web.xml` file contains the Web Application Deployment Descriptor for your application. As the filename extension implies, this file is an XML document, and defines everything about your application that a server needs to know (except the context path, which is assigned by the system administrator when the application is deployed).

The complete syntax and semantics for the deployment descriptor is defined in Chapter 13 of the Servlet API Specification, version 2.2. Over time, it is expected that development tools will be provided that create and edit the deployment descriptor for you. In the meantime, to provide a starting point, a basic web.xml file is provided. This file includes comments that describe the purpose of each included element.

## 3.4 Integration With Tomcat

In order to be executed, a web application must be integrated with, or installed in, a servlet container. This is true even during development. We will describe using Tomcat to provide the execution environment. A web application can be deployed in Tomcat by one of three different approaches:

- Copy unpacked directory hierarchy into a subdirectory in directory `$CATALINA_HOME/webapps/`. Tomcat will assign a context path to your application based on the subdirectory name you choose. We will use this technique in the `build.xml` file that we construct, because it is the quickest and easiest approach during development.
- Copy the web application archive file into directory `$CATALINA_HOME/webapps/`. When Tomcat is started, it will automatically expand the web application archive file into its unpacked form, and execute the application that way. This approach would typically be used to install an additional application, provided by a third party vendor or by your internal development staff, into an existing Tomcat installation. NOTE - If you use this approach, and wish to update your application later, you must both replace the web application archive file AND delete the expanded directory that Tomcat created, and then restart Tomcat, in order to reflect your changes.
- Add a `<Context>` entry in the Tomcat `server.xml` configuration file. This approach is described briefly below, and allows you to position the document root of your web application at some point other than the `$CATALINA_HOME/webapps/` directory. Doing this requires the following steps (for Tomcat 4.0):

Adding a new `<Context>` entry in Tomcat's `server.xml` file involves the following steps (for Tomcat 4.0):

- Open file `$CATALINA_HOME/conf/server.xml` in an editor.
- Navigate to the bottom of the file (after the last existing `<Context>` element).
- Add a new `<Context>` element for your application, using the existing examples as a guide. The following attributes are supported:
  - path. The context path for your application, which is the prefix of a request URI that tells Tomcat which application should be used to process this request. For example, if you set your path to "/catalog", any request URI beginning with "/catalog" will be processed by this application. This attribute is requrired, and must start with a slash ('/') character.
  - docBase. The document root directory for this web application. This can be a relative path (relative to the directory in which Tomcat is started), or an absolute path, to the directory containing your app. On a Windows platform, you MUST use the drive prefix and a colon when specifying an absolute path. This attribute is required.
  - debug. Debugging detail level (from "0" to "9") that defines how verbose Tomcat's logging messages will be when your application is initialized, started, and shut down. The default value is "0" (minimal logging) if you do not specify a different value.
  - reloadable. Set to "true" if you want Tomcat to watch for changes to Java class files in the WEB-INF/classes directory, or JAR files in the WEB-INF/lib directory. If such a change is noted, Tomcat will shut down and reload your application automatically, picking up these changes. The default value ("false") means that such changes will be ignored. NOTE: While this feature is very useful during development, it requires overhead to do the checking. This capability should generally not be used in deployed production applications.

Integrating your app with other servlet containers will be specific to each container, but all containers compatible with the Servlet API Specification (version 2.2) are required to accept a web application archive file.