by November 5, 2002 0 comments

A Web application is a collection of files–HTML, XML, images (GIF, JPEG) and scripts–that reside on an application server (see Application Servers, page 25, PCQuest June 2002) and perform a particular task. If the application is supposed to access a database, the database drivers are included in the Web application. Also included are configuration files (usually in XML) to provide information on the Web application. 

An application server enforces the Web application to be organized in a three-tier architecture: frontend, middleware and backend. It does this by prescribing different directories for the files that present the frontend, files that perform the application logic, files that provide access to the backend (databases) and configuration files. We explain this by taking an example of a simple guestbook Web application. 

Our guestbook Web application consists of a JSP (frontend), a servlet (middleware) and MySQL JDBC drivers (access to backend). The JSP outputs an HTML form where one can fill in his e-mail address, name and comments. The form data after being submitted goes to a Java Servlet (application logic), which appends the form data to a MySQL database (the backend) using the JDBC drivers. Before proceeding further, we assume that you are familiar with Java, JSPs and Servlets. Set up Tomcat as described in Tomcat 4 on PCQLinux 7.1, page 66, PCQuest September 2002. We also assume that you have installed MySQL database on the same machine that runs Tomcat (see Online Databases with MySQL, page 119, PCQuest October 2001). Create a database called guestbook in MySQL and a table named “info” in this database using the following SQL query. 

create table info (name char(20), email char(20), comments varchar(255)) 

Organizing Web application files
The Web applications on Tomcat reside under the directory webapps of the main tomcat directory (/opt/tomcat as per our setup). Create a directory called guestbook under the webapps directory. The guestbook directory is the “root “ directory of our Web application. The files of our Web application will be organized within this directory. Create a subdirectory named “WEB-INF” within guestbook. Create “classes” and “lib” subdirectories with the “WEB-INF” directory. The files of our guestbook Web application will be organized as follows.

Guesbook. It contains all the files that are shown to the user in his browser. This includes all the JSPs (which output HTML), HTML files and images. For our simple guestbook, this directory will contain just one file, namely guestbook.jsp.
guestbook/WEB-INF. This directory contains a configuration file called web.xml 
guestbook/WEB-INF/classes: This directory is the place for the Java class files that perform the application logic. We will place the class file of our Servlet named GuestBookServlet.class in this directory.
guestbook/WEB-INF/lib. For the MySQL JDBC drivers.

The code
The code for guestbook.jsp is as follows.
<br>Please type in the following information:
<form method=”post” action=”servlet/GuestBookServlet” >
<br>Name: <input type=”text” name=”name” size=”20” maxlength=”50”>
<br>Email: <input type=”text” name=”email” size=”20” maxlength=”50”>
<br>Your comments:
<textarea name=”comments” wrap=”virtual”>
<input type=”submit” value=”Submit Info”>

Notice that for our simple example, this JSP is not outputting any dynamic content and is just a simple HTML file. Place this file in the directory /opt/tomcat/webapps/guestbook. 

The code 
for GuestBookServlet is as follows.

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class GuestBookServlet extends HttpServlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException 
PrintWriter out = response.getWriter();
String name = request.getParameter(“name”);
String email = request.getParameter(“email”);
String comments = request.getParameter(“comments”);
Connection con = DriverManager.getConnection(“jdbc:mysql://”); 
Statement stmt = con.createStatement();
stmt.executeUpdate(“insert into info values(‘“+name+”’,’”+email+”’,’”+comments+”’)”);
catch(Exception e)
out.println(“The guestbook data has been submitted to the MySQL database”);

Replace “root” and “pcq” in the above code, with the MySQL user and password in your case. Save the above contents in a file named in the directory/opt/tomcat/webapps/guestbook/WEB-INF/classes. Change to this
directory and compile the Servlet as:

/usr/java/j2sdk1.4.0/bin/javac -classpath /opt/tomcat/common/lib/servlet.jar

Install JDBC drivers
We have given out Type IV MySQL JDBC drivers called MySQLConnector/J (originally called as MM.MySQL) on this month’s CD, in the directory system/cdrom/unltdoss. Extract the archive, change to the resulting directory mysql-connector-java-21.0.14 and copy the file mysql-connector-java-2.0.14-bin.jar to guestbook/WEB-INF/classes. The Web application, when run, will automatically pick up the drivers from this directory. There is no need to set up any

Type in the following contents in a file called web.xml.

<?xml version=”1.0” encoding=”ISO-8859-1”?><!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “”><web-app></web-app>

Save it in the directory guestbook/WEB-INF. Open the file server.xml found in the directory /opt/tomcat/conf and scroll down to the following line.

<Context path=”/manager” docBase=”manager” debug=”0” privileged=”true”/>

After this line add the following line which makes Tomcat aware of our guestbook Web application.

<Context path=”/guestbook” docBase=”guestbook”

Running the Web application
Shutdown and restart Tomcat as


Now we can access the guestbook web application as 

To access the Web application through Apache, open the file httpd.conf, found in /etc/httpd/conf directory. After the line

WebAppDeploy examples conn /examples

add the following line.

WebAppDeploy guestbook conn /guestbook

Restart Apache. 

This was a very simple example of a Web application and have set it up with minimal needed configuration. For more details, see

Shekhar Govindarajan

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.