myServlet WAR

 

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

©2006-2014 by Wayne Pollock, Tampa Florida USA

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.
Step 2 Complile a Servlet.  Enter the servlet code, and use javac to compile it.  You may need to use the -cp command line argument or set CLASSPATH so javac can find the Jar file containing the Servlet API, usually servlet.jar or javaee.jar.  (This depends upon which server you install, and how you configure your system.)  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.  With the correct Jar file listed on CLASSPATH, you can 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 and CSS files, images, JSPs, and even Applets or WebStart apps.  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.  This directory will be created automatically by the jar command, so you don't need to create it manually.

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

The lib directory (not used for this example WAR) contains other JAR files (usually holding packages used by your servlets).  A common example would be xerces.jar, which is an 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 most servers use case-sensitive URLs, which is unusual for Windows.

Step 4 Create the web.xml deployment descriptor.  This XML file tells the servlet container the names of servlets, JSPs, and other components 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 our HelloServet servlet.)

Take a look at the web.xml deployment descriptor for myServletWAR.  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 Microsoft XML Notepad editor (or some other editor) to edit XML files.  XML files can be viewed directly using most web browsers.

Step 5 Create a WAR file.  A WAR file can be easily deployed by coping it into the server's “auto-deploy” folder.  To create a WAR file, use the jar tool from the correct folder (shown in the prompt) 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 HelloServlet.java 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 most servers, 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 the server can find it.  Move the myServletWAR.war file to the directory %TOMCAT_HOME%\webapps\myServletWAR.war if using Tomcat, or %GLASSFISH_HOME%\glassfish\domains\domain1\autodeploy if using Glassfish4.  (“%TOMCAT_HOME%” is the location where Tomcat was installed, and the same for GLASSFISH_HOME.) 

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.

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.