After the WAR has been deployed (installed into A Java server, such as Tomcat or Glassfish) using the steps below, you can click on the URL http://localhost:8080/myServletWAR/hello to run the Hello, World! servlet.
|Step 1||Install Tomcat or Glassfish on your local machine. Click here to see how to install Tomcat under Windows. (Complete Java development setup for Windows, including Glassfish, can be found at Java Setup Directions.) You can install some other servlet container (what I call a Java server) if you wish, as long as it is compatible with the current Servlet API.|
Complile a Servlet.
Enter the servlet code, 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
file to the top-level (document root) directory, as well as other files and
directories such as
Take a look at the
XML files can be created and edited with any text editor, but you may wish to use Microsoft XML Notepad editor (or some other editor) to edit XML files. XML files can be viewed directly using most web browsers.
Create a WAR file.
A WAR file can be easily deployed by coping it into the server's
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
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 the server can find it.
Move the |
Most java servers expand WAR files in the the deployment or other directory. When updating a WAR, you should delete the corresponding directory and WAR file, then deploy the new WAR file version. (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.)
Most Java servers include an administrator's console, where you can deploy, undeploy, or enable/disable WARs (Java web applications).
Once deployed, 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 files that are part of the web application.
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.