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
The
The
The
Create the following directory structure for our web application,
including moving the
If
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 |
|
| 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 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
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 |
|
| 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 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
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.
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.