Home TOC |
![]() ![]() ![]() |
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:
- Server-side utility classes (database beans, shopping carts, and so on). Often these classes conform to the JavaBeans component architecture.
- Static Web content (HTML, image, and sound files, and so on)
- Client-side classes (applets and utility classes)
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:
web.xml -
The Web application deployment descriptor- Tag library descriptor files (see Tag Library Descriptors)
classes
- A directory that contains server-side classes: servlets, utility classes, and JavaBeans componentslib
- A directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes)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:
build.xml
-Ant
build file- mywebapp
.xml
- Optional application configuration filesrc
- Java source of servlets and JavaBeans componentsweb
- JSP pages and HTML pages, imagesThe
Ant
build files (build.xml
) distributed with the examples contain targets to create an unpacked WAR structure in thebuild
subdirectory of mywebapp, copy and compile files into that directory, and invoke Tomcat Manager App commands via specialAnt
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 exampleAnt
targets are:
prepare
- Createsbuild
directory and WAR subdirectories.build
- Compiles and copies the mywebapp Web application files into thebuild
directory.install
- Notifies Tomcat to install an application (see Installing Web Applications) using theAnt
install
task.reload
- Notifies Tomcat to reload the application (see Updating Web Applications) using theAnt
reload
task.remove
- Notifies Tomcat to remove the application (see Removing Web Applications) using theAnt
remove
task.Creating a WAR
You can manually create a WAR in two ways:
- With the JAR tool distributed with the J2SE SDK. You simply execute the following command in the build directory of a tutorial example:
jar cvf mywebapp.war .- With the
Ant
war
taskBoth 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 usedeploytool
, it creates a Web application deployment descriptor based on information entered intodeploytool
wizards and inspectors. To build and package theHello1
application into a WAR namedhello1.war
:
- In a terminal window, go to
docs/tutorial/examples/hello1.
- Run
ant
build
. Thebuild
target will spawn any necessary compilations and copy files to thedocs/tutorial/examples/web/hello1/build
directory.- Start
deploytool
.- Create a Web application called
hello1
.
- Select File
New Web Component.
- Select the Create New Stand-Alone WAR Module.
- Click Browse and in the file chooser, navigate to
docs/tutorial/examples/web/hello1
.- In the File Name field, enter
hello1
.- Click Choose Module File.
- In the WAR Display Name field enter
hello1
.- Add the
greeting
Web component and all of theHello1
application content.
- Click Edit to add the content files.
- In the Edit Contents dialog, select
docs/tutorial/examples/web/hello1/build/duke.waving.gif
and click Add. Navigate toWEB-INF/classes and
selectGreetingServlet.class
, andResponseServlet.class
and click Add. Click OK.- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select
GreetingServlet
from the Servlet Class combo box.- Click Finish.
- Add the
response
Web component.
- Select File
New Web Component.
- 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.- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select
ResponseServlet
from the Servlet Class combo box.- Click Finish.
Home TOC |
![]() ![]() ![]() |