Deploying Web Applications to Tomcat
Pages: 1, 2, 3, 4, 5
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.
<Context path="/onjava" docBase="onjava" debug="0" reloadable="true" />
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
<html>
<head>
<title>OnJava Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" onLoad="document.loginForm.username.focus()">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr>
<td>
<img src="/onjava/images/monitor2.gif"></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<form name="loginForm" method="post" action="servlet/com.onjava.login">
<tr>
<td width="401"><div align="right">User Name: </div></td>
<td width="399"><input type="text" name="username"></td>
</tr>
<tr>
<td width="401"><div align="right">Password: </div></td>
<td width="399"><input type="password" name="password"></td>
</tr>
<tr>
<td width="401"> </td>
<td width="399"><br><input type="Submit" name="Submit"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
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