The JavaTM Web Services Tutorial
Home
TOC
PREV TOP NEXT

Web Application Archives

If you want to distribute a Web application and run it on another server, you package it in a Web application archive (WAR), which is a JAR similar to the package used for Java class libraries and is installed or deployed into a Web container. In addition to Web components, a Web application archive usually contains other files including the following:

Web components and static Web content files are called Web resources.

A Web application can run from a WAR file or from an unpacked directory laid out in the same format as a WAR.

WAR Directory Structure

The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static Web resources are stored.

The document root contains a subdirectory called WEB-INF, which contains the following files and directories:

You can also create application-specific subdirectories (that is, package directories) in either the document root or the WEB-INF/classes directory.

Tutorial Example Directory Structure

To facilitate iterative development and keep Web application source separate from compiled files, the source code for the tutorial examples is stored in the following structure under each application directory mywebapp:

The Ant build files (build.xml) distributed with the examples contain targets to create an unpacked WAR structure in the build subdirectory of mywebapp, copy and compile files into that directory, and invoke Tomcat Manager App commands via special Ant tasks to install, reload, and remove applications. The document Manager App HOW-TO, distributed with the Java WSDP at <JWSDP_HOME>/docs/tomcat/tomcat-managerhowto.html contains information about the manager application. The tutorial example Ant targets are:

Creating a WAR

You can manually create a WAR in two ways:

Both of these methods require you to have created a Web application deployment descriptor.

You can also package an application into a WAR using deploytool. When you use deploytool, it creates a Web application deployment descriptor based on information entered into deploytool wizards and inspectors. To build and package the Hello1 application into a WAR named hello1.war:

  1. In a terminal window, go to docs/tutorial/examples/hello1.
  2. Run ant build. The build target will spawn any necessary compilations and copy files to the docs/tutorial/examples/web/hello1/build directory.
  3. Start deploytool.
  4. Create a Web application called hello1.
    1. Select FileNew Web Component.
    2. Select the Create New Stand-Alone WAR Module.
    3. Click Browse and in the file chooser, navigate to docs/tutorial/examples/web/hello1.
    4. In the File Name field, enter hello1.
    5. Click Choose Module File.
    6. In the WAR Display Name field enter hello1.
  5. Add the greeting Web component and all of the Hello1 application content.
    1. Click Edit to add the content files.
    2. In the Edit Contents dialog, select docs/tutorial/examples/web/hello1/build/duke.waving.gif and click Add. Navigate to WEB-INF/classes and select GreetingServlet.class, and ResponseServlet.class and click Add. Click OK.
    3. Click Next.
    4. Select the Servlet radio button.
    5. Click Next.
    6. Select GreetingServlet from the Servlet Class combo box.
    7. Click Finish.
  6. Add the response Web component.
    1. Select FileNew Web Component.
    2. Click the Add to Existing WAR Module radio button and select hello1 from the combo box. Since the WAR contains all of the servlet classes, you do not have to add any more content.
    3. Click Next.
    4. Select the Servlet radio button.
    5. Click Next.
    6. Select ResponseServlet from the Servlet Class combo box.
    7. Click Finish.
Home
TOC
PREV TOP NEXT