ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Creating a Web Application with Ant and Tomcat 4

by Paul Wood
01/08/2003

Which Technologies to Use?

I have decided to use Tomcat 4 Servlet/JSP Container technologies to implement a Web application. This still leaves many options, and choosing between the various available technologies is not easy. For this article, I have chosen to keep it simple and use Java Server Pages (JSPs) in combination with Java classes.

You should really avoid using JSPs to implement complex logic; such applications may be fun to write, but can be frustrating to debug and almost impossible to understand and maintain. A really good solution is to use JSPs to handle the Web page presentation (because that is what JSPs are good for), and Java to implement complex logic, such as database access. This way, the software will be easier to write and debug, and later the code will be easier to understand and maintain.

Related Reading

Ant: The Definitive Guide
By Jesse E. Tilly, Eric M. Burke

The Web application can be served on a dedicated Web server or on a personal computer running a Windows, Linux, or Mac OS X operating system. It requires a Java runtime environment (JDK 1.2 or newer), a recent version of the Tomcat Servlet/JSP Container (version 4 or better), and the Ant development tool (version 1.4 or later). Ant is used to create the Web application's Web Archive (WAR) file, and to install and deploy the WAR file into Tomcat, configuring the javax.sql.DataSource relational database interface in the process. All of these packages are available on the Internet at no cost.

The Web application also requires a database. Almost any relational database will do, if it accepts SQL and has a JDBC driver. MySQL has a good reputation and fits the bill exactly. You can get the latest MySQL and its Connector/J JDBC Driver from MySQL.com.

To allow Ant to work with Tomcat's manager software, copy catalina-ant.jar from Tomcat's server/lib directory into Ant's lib directory. To give database access to Tomcat and its installed Web applications, copy your database's JDBC Driver file into Tomcat's common/lib directory. Finally, you must set up the Tomcat admin and manager roles, and the usernames and passwords. Edit the tomcat-users.xml file in Tomcat's conf directory, so that it looks like this:

<?xml version='1.0'?>
    <tomcat-users>
        <role rolename="admin"/>
        <role rolename="manager"/>
        <user username="tomcatusername" password="tomcatpassword"
                 roles="admin,manager"/>
    </tomcat-users>

For your information, I used Tomcat 4.1.12, Ant 1.5.1, MySQL 3.23.52, and Connector/J 3.0.1-beta running on an Apple Cube with Mac OS X 10.2.1 (which includes Java Development Kit 1.3.1) to develop this Web application. When Mac OS X 10.2.2 was released, I upgraded without problem. When Tomcat 4.1.17 was released, again I upgraded without problem.

The Web Application

The Web application used to demonstrate the technologies used in this article is a simple address book application, called AddressBook. It is not the purpose of this article to create the ultimate address book. It is just an example.

AddressBook's Java Classes

AddressBook has three Java classes, detailed below. Figure 1 shows how the classes are used within the Tomcat Servlet/JSP Container.

Figure 1

AddressBook.ContextListener

AddressBook.ContextListener is a servlet context listener that will be called when AddressBook starts up and shuts down. This is configured in AddressBook's deployment descriptor file, web.xml. When AddressBook is starting up, an instance of the AddressesDB object is created and stored as a context attribute; when it is shutting down, the AddressesDB object is retrieved from the context attribute and the database is closed. In between, the JSPs retrieve the AddressesDB object from the context attribute whenever they need to access the database.

See ContextListener.java for the complete, annotated source code.

AddressBook.AddressesDB

AddressBook.AddressesDB is a class that represents the addresses database. Its constructor establishes a connection to the database, and the session can be shared across several Web sessions. The class provides several database-access methods:

  • getAddress(id) returns the address identified by the id, or null if the address is not found.
  • addAddress(address) adds an address and returns the number of rows affected.
  • deleteAddress(id) deletes the address identified by the id and returns the number of rows affected.
  • modifyAddress(address) modifies an address and returns the number of rows affected.
  • getAddresses() returns a sorted Collection of all of the addresses in the database, or null if the addresses database is not found.
  • close() closes the database connection.

See AddressesDB.java for the complete, annotated source code.

AddressBook.Address

AddressBook.Address is a class that represents an address. Classes that collect data together are still a good idea when programming JSPs, so this class is a good idea for the AddressBook application.

The AddressBook.Address constructor stores the address fields in the object. This class also includes the usual get methods to retrieve address fields, two additional get methods to retrieve combined fields, and a method to compare addresses:

  • Address(id, surname, firstname, street, district, city, postcode)
  • getId()
  • getSurname()
  • getFirstname()
  • getStreet()
  • getDistrict()
  • getCity()
  • getPostcode()
  • getFullname() returns the firstname + " " + surname
  • getFulladdress() returns street + " " + district + " " + city + " " + postcode
  • compareTo(address) returns a negative integer, zero, or a positive integer, depending on whether surname and firstname is greater than, equal to, or less than this surname and firstname, ignoring case considerations. It is used when sorting a Collection of addresses.

See Address.java for the complete, annotated source code.

AddressBook's Java Server Pages

AddressBook has seven JSPs. Each represents a Web page that performs an independent operation on the AddressBook's database. The state of the database in each page is independent of the state of the database for the previous page. If the database changes due to another user's request, the Web page will detect this and react accordingly. For example, if you are modifying an address and another user deletes that address before you confirm the modification request, the JSP will notify you of the failture to modify the now-non-existent address.

Figure 2 shows the logic flow between the JSPs.

Figure 2

Home.jsp

Home.jsp is the home page of the AddressBook Web application, and is the first page displayed when a user enters the AddressBook Web application. It displays a table showing all of the addresses in the Addresses database. Each row of the table shows an individual address, and links to delete or to modify that address. The bottom of the page has a link to add a new address. See the web.xml notes for information on how this is configured.

See Home.jsp for the complete, annotated source code.

RequestAdd.jsp

RequestAdd.jsp displays an address form in which you enter the new address fields. At the bottom of the Web page is an abort link for aborting the request.

  • Submitting the form transfers control to DoAdd.jsp with the new address fields (surname, firstname, street, district, city, and postcode).
  • Aborting the form transfers control to Home.jsp.

See RequestAdd.jsp for the complete, annotated source code.

DoAdd.jsp

DoAdd.jsp displays the new address fields it receives. It then attempts to add the new address to the database and displays the success or failure of the attempt. At the bottom of the Web page is a continue link.

  • Selecting the continue link transfers control to Home.jsp.

See DoAdd.jsp for the complete, annotated source code.

RequestDelete.jsp

RequestDelete.jsp reads the address corresponding to the id it received. It then displays the address fields in a table so that you can ensure that the correct address is to be deleted. At the bottom of the Web page are two links: a continue link for continuing with the request, and an abort link for aborting the request.

  • Selecting the continue link passes to DoDelete.jsp the id of the address to be deleted.
  • Selecting the abort link passes control to Home.jsp.

See RequestDelete.jsp for the complete, annotated source code.

DoDelete.jsp

DoDelete.jsp reads the address whose id was passed to it. It then displays the address in a table, attempts to delete the address from the database, and displays the success or failure of the attempt. At the bottom of the Web page is a continue link.

  • Selecting the continue link transfers control to Home.jsp.

See DoDelete.jsp for the complete, annotated source code.

RequestModify.jsp

RequestModify.jsp displays an HTML form with the current address values. It also features a submit button and a link to abort the request.

  • Submitting the form transfers control to DoModify.jsp with its address fields (id, surname, firstname, street, district, city, and postcode).
  • Selecting the abort link transfers control to Home.jsp.

See RequestModify.jsp for the complete, annotated source code.

DoModify.jsp

DoModify.jsp displays the modified address fields it receives in a table. It then attempts to modify the address in the database and displays the success or failure of the attempt. At the bottom of the Web page is a continue link.

  • Selecting the continue link passes control to Home.jsp.

See DoModify.jsp for the complete, annotated source code.

Pages: 1, 2, 3, 4

Next Pagearrow