|
In this article we are going to cover the
deployment of web applications using Tomcat. We are performing a manual
deployment to fully explain the steps involved when deploying a web
application.
The best way to describe the deployment process
is to create a web application of our own that includes the important
components found in most Java web applications; then package it for
deployment. The following sections will take you through the steps
involved in deploying a web application. The name of our web
application will be onjava.
In this article, we
- create the web application directory structure,
- create a web application ServletContext,
- add JSPs,
- add Servlets,
- add Tag Libraries, and
- create and deploy a WAR file.
Creating the Web Application Directory Structure
The first thing you need to do is create the directory structure
that will contain the application. We discussed this structure in Part
1, Java Web
Applications, and I include the relevant details in Table 1.
Table 1. The Web Application Directory
Structure |
Directory |
Contains |
/onjava | This is the root
directory of the web application. All JSP and XHTML files are stored
here. |
/onjava/WEB-INF |
This directory
contains all resources related to the application that are not in the
document root of the application. This is where your web application
deployment descriptor is located. Note that the WEB-INF directory is
not part of the public document. No files contained in this directory
can be served directly to a client. |
/onjava/WEB-INF/classes | This directory is
where servlet and utility classes are located. |
/onjava/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. |
The name of our web application, onjava, is the root
of our directory structure.
While in development I suggest creating the directory directly in
the Tomcat /webapps directory. When it comes time for
deployment, you can package your web application into a WAR file and
go though the production deployment process.
The last step in creating the web application directory structure
is adding a deployment descriptor. At this point you'll be creating a
default web.xml file that contains only the DTD, describing the
web.xml file, and an empty element. Listing 1
contains the source for a default web.xml file.
Listing 1 web.xml
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
Now copy this file to the TOMCAT_HOME/onjava/WEB-INF/
directory, and we'll begin adding web application components to it in
the following sections. Creating a Web Application ServletContext
After you've created the web application directory structure, you
must add a new ServletContext to Tomcat. The
ServletContext defines a set of methods that are used by
components of a web application to communicate with the servlet
container. The ServletContext acts as a container for
the web application. There is only one ServletContext
per web application. We will discuss the relationship between a
ServletContext and its web application in much more
detail in Part 4, "Web Applications and the ServletContext." To add a new ServletContext to Tomcat you need to add
the following entry to the TOMCAT_HOME/conf/server.xml
file, setting the values for the path and
docBase to the name of your web application. Notice again
that the name we are using is onjava.
There are two things here we need to focus on. The first,
path="/onjava", tells the servlet container that all
requests with /onjava appended to the server's URL belong to the
onjava web application. The second,
docBase="onjava", tells the servlet container that the
web application exists in the /onjava directory.
Adding JSPs
Now that you have created the web application directories and added
ServletContext, you can add server-side Java
components. The first components we are going to add are JSPs.
The first JSP will include a simple login screen. Listing 2
contains the source for the login.jsp page.
Listing 2 login.jsp
OnJava Demo
As you look at this JSP, you'll see nothing very special about
it. The only thing you should pay attention to is the
action of the form. It references a servlet in the
package com.java named login. This servlet
will retrieve the username-password parameters from the request and
perform its own processing.
There isn't much to deploying a JSP. You just copy it to the public
directory of your web application, which in this case is
TOMCAT_HOME/webapps/onjava/. Any images that it
references should be placed in an images folder that you have created
in the /onjava directory.
To see this JSP in action, open the following URL in a browser:
http://localhost:8080/onjava/login.jsp The second JSP is the target JSP referenced by the servlet defined
in the following section. This JSP will retrieve the request attribute
USER that was added to the request with the following servlet. It will
then output the String value of the attribute. Listing 3 contains the
source for the target JSP.
Listing 3 welcome.jsp
OnJava Demo
|
 |
Welcome : <%= request.getAttribute("USER") %>
|
|
|