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
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
root permissions. When requested, enter the password.
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
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
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 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
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,
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
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
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.
the complete, annotated source code.
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.
build.xml for the complete,
annotated source code.
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.
Logger entry specifies that Tomcat should log AddressBook
events into a text file stored in Tomcat's
logs directory. Here is
Logger entry to name the log file
YYYY-MM-DD is the log's date:
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_AddressBook_log." suffix=".txt" timestamp="true"/>
Resource entry, together with a
entry, specifies that Tomcat should establish a
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
<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
context.xml for the complete, annotated source code.
web directory contains the Web application's seven JSPs.
DoModify.jsp for the complete, annotated
the Web application deployment descriptor for AddressBook. It contains two key
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>
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>
web.xml for the complete,
annotated source code.