Creating a Web Application with Ant and Tomcat 4
Pages: 1, 2, 3, 4
AddressBook's Addresses Database Table
Setting up the database depends on the database software being used. You
will need to amend these instructions if you are not using MySQL. AddressBook
employs a single table called Addresses in a database called
Public. The Addresses table has seven fields:
id, the primary key, a numeric field that is automatically incremented by the database software.surname, a 24-character field that holds the surname of the owner of the address.firstname, a 24-character field that holds the first name of the owner of the address.street, an 80-character field that holds the address' first line.district, an 80-character field that holds the address' second line.city, a 40-character field that holds the address' city.postcode, a 10-character field that holds the address' postcode.
Let's set up the MySQL database. Start the mysql command-line
utility with root permissions. When requested, enter the password.
Create the Public database and grant the
mysqlusername permissions on that database, with a permission of
mysqlpassword. The commands are:
# mysql -u root -p
mysql> create database Public;
mysql> grant all privileges on Public.* to mysqlusername@localhost \
identified by 'mysqlpassword' with grant option;
mysql> flush privileges;
Next, create the Addresses table in the Public
database.
mysql> create table Addresses ( \
id int(8) primary key auto_increment, \
surname varchar(24) not null, \
firstname varchar(24) not null, \
street varchar(80) not null, \
district varchar(80) not null, \
city varchar(40) not null, \
postcode varchar(10) not null \
);
Commit the changes with the commit; command. To verify the
table's configuration,issue the describe Addresses; command. You
should get the following:
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(8) | | PRI | NULL | auto_increment |
| surname | varchar(24) | | | | |
| firstname | varchar(24) | | | | |
| street | varchar(80) | | | | |
| district | varchar(80) | | | | |
| city | varchar(40) | | | | |
| postcode | varchar(10) | | | | |
+-----------+-------------+------+-----+---------+----------------+
Now you can insert a row of data to test the database.
mysql> insert into Addresses (surname, firstname, street, district, \
city, postcode) values ("Smith", "John", "1, The High Street,", \
"Downtown,", "Metropolis.", "X99 9XX");
mysql> commit;
mysql> select * from Addresses;
This should produce the following output:
+----+---------+-----------+--------------+-----------+-------------+----------+
| id | surname | firstname | street | district | city | postcode |
+----+---------+-----------+--------------+-----------+-------------+----------+
| 1 | Smith | John | 1, The | Downtown, | Metropolis. | X99 9XX |
| | | | High Street, | | | |
+----+---------+-----------+--------------+-----------+-------------+----------+
Notice that the address id has been automatically assigned a value of 1. AddressBook's database is now ready for use.
Configuring AddressBook's Development Directory
We are now ready to configure the development directory where AddressBook's source
files are held, and the war directory where the Web application
will be built and where the Web application's Web archive file will be
held.
Download the AddressBook source files archive
(AddressBook.zip). Unzip the archive. You now have the
application's source files in a directory structure ready for processing by the
Ant development tool, as you can see in Figure 3. There is no need
to put AddressBook's development directory anywhere special -- I recommend that
you put it somewhere in your home directory structure.

Lets have a look at these files:
AddressBook/build.properties
AddressBook/build.properties contains a number of configuration settings that are read into the
AddressBook/build.xml file. To configure the build, you should
edit the smaller build.properties file. Leave the more complex
build.xml file alone as much as possible.
app.name=AddressBook
tomcat.home=/usr/local/jakarta-tomcat-4.1.12
manager.url=http://localhost:8080/manager
username=tomcatusername
password=tomcatpassword
You do not need to change the app.name parameter now. If you
later reuse these files to create another Web application, you can change
app.name to reflect the name of the new application. Assuming that
you are using a Tomcat 4 instance running on your local machine, you should not
need to change the manager.url parameter.
You will probably need to change the tomcat.home parameter to
point at your Tomcat home directory. The above setting assumes that Tomcat
4.1.12 has been installed in the /usr/local directory. Ant uses
this parameter to set up a CLASSPATH for compiling AddressBook's Java classes,
with Tomcat's common/lib .jar files. (These .jar files are
automatically available at runtime to both Tomcat and the Web application and
do not need to be copied into the Web application's WEB-INF/lib
directory.)
Tomcat has a similar directory: shared/lib. These .jar files are
available at runtime to the Web application, but not to Tomcat itself.
You will need to change the tomcatusername and
tomcatpassword parameters so that they match those in Tomcat's
conf/tomcat-users.xml file. Ant uses these parameters, as well as
manager.url, to establish its authority to install the AddressBook
Web application into the Tomcat environment.
See build.properties for
the complete, annotated source code.
AddressBook/build.xml
AddressBook/build.xml contains the Ant configuration data. build.xml implements many Ant "targets."
Here are the more useful targets:
ant buildbuilds the Web application.ant installinstalls the Web application for testing.ant removeuninstalls the Web application.ant deploypermanently installs the Web application.ant undeploypermanently undeploys the Web application.
See build.xml for the complete,
annotated source code.
AddressBook/context.xml
AddressBook/context.xml is
installed into Tomcat by Ant when processing install, reload, remove, deploy,
and undeploy requests. It contains the details of AddressBook's "context," or
the parameters that Tomcat uses when executing AddressBook.
A Logger entry specifies that Tomcat should log AddressBook
events into a text file stored in Tomcat's logs directory. Here is
the Logger entry to name the log file
localhost_AddressBook_log.YYYY-MM-DD.txt, where
YYYY-MM-DD is the log's date:
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_AddressBook_log." suffix=".txt" timestamp="true"/>
A Resource entry, together with a ResourceParams
entry, specifies that Tomcat should establish a
javax.sql.DataSource named jdbc/Public for
AddressBook. The following settings establish the
javax.sql.DataSource as a
org.apache.commons.dbcp.BasicDataSourceFactory class, the URL as
jdbc:mysql://localhost:3306/Public?autoReconnect=true, a JDBC
Driver Class of org.gjt.mm.mysql.Driver, a username of
mysqlusername, and a password of mysqlpassword:
<Resource name="jdbc/Public" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/Public">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/Public?autoReconnect=true</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>mysqlusername</value>
</parameter>
<parameter>
<name>password</name>
<value>mysqlpassword</value>
</parameter>
... [omitted] ...
</ResourceParams>
The above code creates the javax.sql.DataSource as part of the
Web application's context. Alternatively, where the
javax.sql.DataSource is to be used by more than one Web
application, it may be defined as a global resource outside of the Web
application's context with the Tomcat
Administration software.
See context.xml for the complete, annotated source code.
AddressBook/src/*.java
The src directory contains the Web application's three Java
classes. See Address.java, AddressesDB.java, and ContextListener.java for the
complete, annotated source code.
AddressBook/web/*.jsp
The web directory contains the Web application's seven JSPs.
See Home.jsp, RequestAdd.jsp, DoAdd.jsp, RequestDelete.jsp, DoDelete.jsp, RequestModify.jsp, and DoModify.jsp for the complete, annotated
source code.
AddressBook/web/WEB-INF/web.xml
AddressBook/web/WEB-INF/web.xml is
the Web application deployment descriptor for AddressBook. It contains two key
parameters.
The listener parameter identifies the listener Java class
AddressBook.ContextListener, which will be called during application
startup and shutdown.
<listener>
<listener-class>AddressBook.ContextListener</listener-class>
</listener>
The welcome-file-list parameter controls the entry point for
AddressBook. The following code sets Home.jsp as AddressBook's only entry point.
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
See web.xml for the complete,
annotated source code.