George Koutsogiannakis
Spring 2009
How to deploy a simple web site application using Tomcat:
NOTE: The instructions vary for various versions of Tomcat!!!!! Do a search on
the particular Tomcat version documentation that you are installing!!!!!!
DEPLOY WITHOUT CREATING A WAR FILE (Note: Not always successful).
1. Go to webapps folder of Tomcat
2. Create a root context in other words a new folder with the name of yourweb site.
3. Within the root context create another folder with the name WEB-INF
4. Within WEB-INF create another folder named classes
5. Within the WEB-INF folder place a copy of the web.xml file from another webapp example or a simple web.xml with contents:
If servlets files are going to be used then the web.xml file needs to identify the servlet in the tag.
See example web.xml that comes with Tomcat (or read the lecture presentation)
6. Within the root context place your html file or if you have java server pages files (.jsp) you also place them here
(directly under the root context folder).
7. Within the root context place any applet compiled files (directrly under the context root folder)
If you have jar files (such as in the case of a trusted applet) place them in path root-context/WEB-INF/lib/.
If your application has servlets place the servlet files in path root-context/WEB-INF/classes/
you could create packages also and place them in the above paths.
8. Open Tomcat's web site via address http://localhost:8080
9. Click on Tomcat Manager link. You will be asked for your pasword which you created
when you installed Tomcat.
10. On the address bar type: http://localhost:8080/manager/deploy?path=/name_of_root_context_you_created
11. If the application was deployed properly you should see O.K. displayed.
12. Now you can call your application via Tomcat by opening your browser and
typing http://localhost:8080/nameofrootcontext/nameofhtmlfile.html.
----------------------------------------------------------------------------------------------------------------------
WEB APPLICATION
--------------------------------------------------------------------------------------------------------------------
Normally a web application should be developed outside Tomcat. If you are creating a war file the deployment procedure
will automatically create the proper folders and names within webapps directory of Tomcat!!!
Each web application has one and only one ServletContext. This relationship is controlled by the servlet container and guarantees
that web applications will not clash when storing objects in the ServletContext.
The following items can exist in a web application:
* Servlets
* JavaServer Pages
* Utility Classes
* Static Documents including, XHTML, images, etc.
* Client side classes
* Meta information that describes the web application
The container that holds the components of a web application is the directory structure in which it exists.
The first step in creating a web application is creating this structure.
The following table contains a sample web application, named "MyWebSite".
Each one of these directories should be created from the of the servlet container.
An example of a , using Tomcat, would be Program Files/Apache Software Foundation/Tomcat5.5/webapps.
The Web Application Directory Structure
1) webapps/MyWebSite
This is the root directory (root context) of the web application. All JSP, .class and XHTML files are stored here.
2) webapps/MyWebSite/WEB-INF
This directory contains all resources related to the application that are not in the document root of the application.
The file web.xml (deployment descriptor) resides here. No files contained in this directory can be served directly to a client.
3) webapps/MyWebSite/WEB-INF/classes
This directory is where servlet and utility classes are located.
4) webapps/MyWebSite/WEB-INF/lib
This directory contains Java Archive files that the web application depends upon.
For example, this is where you would place a JAR file that contained a JDBC driver.
As you look over the contents of the web application's directory structure,
you will notice that web applications allow for classes to be stored in both the /WEB-INF/classes and
/WEB-INF/lib directories. Of these two, the class loader will load classes from the /classes directory first
followed by the JARs in the /lib directory. If you have duplicate classes in both the /classes and /lib directories,
the classes in the /classes directory will be used.
The Web application deployment descriptor
At the heart of all web applications is a deployment descriptor.
The deployment descriptor is an XML file named web.xml located in the //applicationname/WEB-INF/ directory.
It describes configuration information for the entire web application. For our application the location of the web.xml file is
in the //MyWebSite /WEB-INF/ directory.
The information that is contained in the deployment descriptor includes the following elements:
* ServletContext Init Parameters
* Localized Content
* Session Configuration
* Servlet / JSP Definitions
* Servlet / JSP Mappings
* Mime Type Mappings
* Welcome File list
* Error Pages
* Security
The following code snippet contains a limited example of a web application deployment descriptor.
The OnJava App
30
TestServlet
com.onjava.TestServlet
1
name
value
In this example we are setting three application level elements. The first of the application level elements is the .
This element simply describes the name of the web application. It is functionally inoperative.
The second web application level element is the element.
This element controls the lifetime of the application's HttpSession object. The value that we have used above
tells the JSP/Servlet container that the HttpSession object will become invalid after 30 minutes of inactivity.
The last application level element that we have defined is the element.
This element defines a servlet and its properties. We will further define the elements when we discuss deploying Servlets
and JSPs to Tomcat in a subsequent article.
Packaging a Web application using WAR files
Now that we know what a web application is, we can package it for deployment. The standard method for packaging web applications
is to use a Web ARchive file (WAR). You can create a WAR file by using Java's archiving tool jar.
An example of this would be to change to the root directory of your web application and type the following command:
jar cvf MyWebSite.war . (open DOS command window at the level where your folder MyWebSite exists and from inside the folder use the jar command).
This command will produce an archive file named MyWebSite.war that will contain your entire web application.
Creating and Deploying a WAR File (NOTE: STEPS 1 TO 12 AT THE TOP SHOULD WORK WITHOUT A WAR FILE.USE THIS
APPROACH AS AN ALTERNATE WAY)
When your web application is ready for deployment, you need to package it for distribution.
2. Go to the folder where you developed your web application.Archive the web application:
jar cvf MyWebSite.war . (go to the directory where your folder MyWebSite exists, assuming
that you develop the web site outside of Tomcat-- don't forget the dot at the end!)
3. Deploy from Manager site of Tomcat. At the bottom of the manager site where it says "War File to Deploy"
Browse to where your war file is located in your directory structure and then press deploy.
A O.K. message and an entry onto the list of deployed webapps means successful deployment.
3. Change to the root directory of your web application. In this case the root directory would be TOMCAT_HOME/webapps/MyWebSite/.
You should be able to see the war file and the web app called for instance MyWebSite.
NOTE: Watch out for double naming i.e webapps/MyWebSite/MyWebSite/index.html in which case in order to call
the web site from a Browser you will have to enter:
http://localhost:8080/MyWebSite/MyWebSite/index.html