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.|
Complile a Servlet.
Enter the servlet and use
C:\Temp> javac HelloServlet.java
Create the web application directory
All web applications use a standard hierarchy of subdirectories and
The top level directory, known as the web application document root,
is named for the web application.
In our case this is
Create the following directory structure for our web application,
including moving the
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
A copy of the
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.
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
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
(This WAR also contains the servlet
a top level HTML file called index.html
so you can see something reasonable if you use the URL
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.
Deploy the WAR.
The WAR file (or directory hierarchy) must be copied into the correct
directory so Tomcat can find it.
Move the |
Note that Tomcat expands WAR files in the
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.
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
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.
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" is the location you used to
install the JBOSS server.