myServlet WAR

 

A detailed walk-thru of the creation and deployment of a Web application ARchive (WAR) file containing a Servlet.

©2006 by Wayne Pollock, Tampa Florida USA

After the WAR has been deployed (installed into Tomcat) using the steps below and Tomcat has been restarted, you can click on the URL http://localhost:8080/myServletWAR/hello to run the Hello, World! servlet in the WAR.

Step 1 Install Tomcat on your local machine.  Click here to see how to install Tomcat under Windows .  You can install some other servlet container if you wish as long as it is compatible with the 2.2 (or newer) ServletAPI.
Step 2 Complile a Servlet.  Enter the servlet and use javac to compile it.  You may need to use the -cp command line argument or set CLASSPATH so javac can find the servlet.jar file.  (This depends upon how you installed Tomcat.)  In this example we create a servlet called HelloServlet.  This servlet simply displays a customized "Hello, World" message.  (It reports the IP address of where the web request was made.)  In real life your servlets will likely use JDBC to query or update a database.  Compile the servlet with the command:
C:\Temp> javac HelloServlet.java
Step 3 Create the web application directory hierarchy.  All web applications use a standard hierarchy of subdirectories and special files:

The top level directory, known as the web application document root, is named for the web application.  In our case this is myServletWAR.  This directory contains HTML files, images, JSPs, and even Applets.  You can create your own subdirectories here too, such as for images.

The META-INF directory contains the manifest.mf file and possibly some other files related to the JAR.

The WEB-INF directory contains all your server-side classes and packages, such as the servlets defined in this WAR.

The lib directory contains JAR files holding packages used by your WAR applications.  (A common example would be xerces.jar, which is the Apache package for XML file processing.)

Create the following directory structure for our web application, including moving the HelloServlet.class file to the indicated directory:

C:\Temp\
   myServletWAR\
      META-INF\
      WEB-INF\
         classes\
            HelloServlet.class
         web.xml

If HelloServlet was in a package of com.wpollock.myServletWAR, then HelloServlet.class would be placed in the directory myServletWAR\WEB-INF\classes\com\wpollock\myServletWAR\HelloServlet.class instead of in classes directly.

Some web applications may have additional files and directories.  You can add an index.html file to the top-level (document root) directory, as well as other files and directories such as Sources (containing the HelloServlet.java file), images, etc.  Please note that Tomcat, like Apache, uses case-sensitive URLs.

Step 4 Create the web.xml deployment descriptor.  This XML file tells the Tomcat (or other) servlet container the names of servlets and JSPs and JavaBeans that comprise the web application.  You can also define a mapping of URLs (really URIs) to servlets.  (In our case we map the URL ".../hello" to ".../servlet/HelloServlet".)

A copy of the web.xml deployment descriptor for myServletWAR can be found by clicking here.  Install this file in the subdirectory indicated in step 3.

XML files can be created and edited with any text editor, but you may wish to use this handy Microsoft XML Notepad editor to edit XML files.  XML files can be viewed directly using Microsoft Internet Explorer v5.5.

Step 5 Create a WAR file.  The directory structure created previously can be deployed in Tomcat directly (which may be useful for development).  However the more common deployment is to create a single WAR file and deploy that instead.  To create a WAR file, use the jar tool as follows:
C:\Temp\myServletWAR> jar -cvf myServletWAR.war .

(Don't forget the period at the end of the command line!)  This will result in the WAR file similar to the one you can download here.  (This WAR also contains the servlet source code, a top level HTML file called index.html so you can see something reasonable if you use the URL http://localhost:8080/myServletWAR/, and a JSP example MilliSec.jsp.)

Note that the resulting JAR file is compressed as normal.  While this is not a problem for applets, it does slow down a server which must uncompress the WAR before using it.  An additional jar command line argument of 0 will cause the jar tool to create an uncompressed archive instead.  (When using Tomcat, your WAR files are expanded when the server first uses them and saves the result, so using compression is no problem.  However it might be a problem for other servlet containers, which may need to access the WAR each time.)

Step 6 Deploy the WAR.  The WAR file (or directory hierarchy) must be copied into the correct directory so Tomcat can find it.  Move the myServletWAR.war file to the directory %TOMCAT_HOME%\webapps\myServletWAR.war.  "%TOMCAT_HOME%" is the location where Tomcat was installed.  For the Tomcat installion described earlier the full pathname should be C:\jdk\jakarta\tomcat\webapps\myServletWAR.war.

Note that Tomcat expands WAR files in the webapps directory.  When updating the WAR file you should delete the corresponding directory.  (The latest version of Tomcat has a setting to automatically do this when the WAR file is updated, but this causes Tomcat to run slower and this feature should not be relied upon.)

Some previous versions of Tomcat required a server configuration file to be updated in order to have Tomcat recognize a newly added web application.  In the current version this is no longer true.  It is enough to put the WAR file in the correct location.  As of Tomcat5, you no longer even need to restart!

Once this is done you should be able to run the servlet by using the proper URL shown at the top of this document.  In general the URL for a web application is the server part (including the port number) and the name of the web application.  This can be followed by any HTML or JSP documents that are part of the web application.  Note that with older versions of TomCat, servlets can be run directly by using a special URL.  In our example this would be:  http://localhost:8080/myServletWAR/servlet/HelloServlet.  However the way this worked led to security holes and other problems, and is no longer supported by default in newer versions of TomCat and shouldn't be used.

Deploy Web applications in BEA Systems' WebLogic Server

WebLogic is a popular alternative to the Tomcat server (which is the standard, reference implemetation of a Java Servlet Container).  You develop your WAR files the same way for all servlet containers, but the deployment methods differ, since to deploy a WAR means to configure the server in some way and each server has a different method for this.

For WebLogic, copy your WAR file into the config/mydomain/applications subdirectory of your WebLogic Server distribution (where mydomain is the name of your WebLogic Server domain).  You can also copy the entire expanded Web application directory structure but using a WAR is easier and more common.  As soon as the files have been copied, WebLogic Server notices the addition and it can be used immediately.

Deploy Web applications in JBOSS Server

Deployment for JBOSS is about the same as for WebLogic.  You copy a WAR (or EAR or any JAR file) into the correct directory.  JBOSS will notice the change and use the new WAR immediately.  Currenly (version 4) the default deployment directory is $JBOSS_HOME/server/default/deploy, where "$JBOSS_HOME" is the location you used to install the JBOSS server.