Creating a Web Application with Ant and Tomcat 4
Pages: 1, 2, 3, 4
Building, Installing, Deploying, and Running AddressBook
To install the address book, change to the AddressBook directory, and issue the
install command to Ant:
$ cd AddressBook
$ ant install
If the install failed, check that the tomcat-users.xml file in
Tomcat's conf directory is set up correctly, and that
catalina-ant.jar in Tomcat's server/lib directory has
been copied into Ant's lib directory.
Note that ant install automatically triggers a number of other
Ant targets, in this sequence: ant init, ant prepare, ant build, and ant
package, as follows:
ant initinitializes the time stamp.ant preparecreates awardirectory structure in the AddressBook directory, as follows: awardirectory, awar/WEB-INF, awar/WEB-INF/classesdirectory, and awar/WEB-INF/libdirectory.ant buildbuilds the Web Application by copying thejspfiles into thewardirectory, copying thecontext.xmlfiles into thewar/META-INFdirectory, copying theweb.xmlfile into thewar/WEB-INFdirectory, and compiling any Java files into thewar/WEB-INF/classesdirectory.ant packagecreates the Web archive file from thewardirectory. The Web archive file is a .jar file and is created using thejarapplication.Finally,
ant installinstalls the Web archive file into Tomcat using thewar/META-INF/context.xmlconfiguration file. Note that there is no need to log in as root or Tomcat to install AddressBook in Tomcat; your normal user account will do fine.antuses thetomcatusernameandtomcatpasswordspecified inAddressBook/build.propertiesto access Tomcat in a secure manner.
This is controlled by the depends option; see AddressBook/build.xml for details.
Figure 4 shows the war directory structure populated with the
various files that compose the AddressBook Web application.

Point your browser at http://localhost:8080/AddressBook
to test the AddressBook Web application. You can see AddressBook's Home Page in
Figure 5. If it failed to execute correctly, check that you have put your
database's JDBC driver file in Tomcat's common/lib directory.
Finally, check in Tomcat's logs directory -- it contains various
text files that will help you investigate the problem.

Try to add a new address, then modify it, then delete it, then add another
new address. You will see that the id keeps incrementing
automatically.
The Development Cycle
When AddressBook was installed, Tomcat called
AddressBook.ContextListener.contextInitialized, which created an
instance of AddressBook.AddressesDB. The
AddressBook.AddressesDB constructor established a connection using
the jdbc/Public DataSource.
AddressBook.ContextListener.contextInitialized then saved the
instance of AddressBook.AddressesDB as a servlet attribute called
addressesDB.
When AddressBook's Home.jsp was first called up, Tomcat
compiled and executed it. Home.jsp retrieved the servlet attribute
called addressesDB and acquired the instance of
AddressBook.AddressesDB. Home.jsp then read the
addresses from the database and displayed them. As you proceeded through the
other JSPs for the first time, they were also compiled and executed. The other
JSPs access the database in the same way as Home.jsp.
Issue ant stop to stop the AddressBook Web application. Tomcat
will automatically call
AddressBook.ContextListener.contextDestroyed and retrieve the same
addressesDB servlet attribute and acquire the same instance of
AddressBook.AddressesDB.
AddressBook.ContextListener.contextDestroyed will then close the
database connection and remove the addressesDB servlet
attribute.
Issue ant start to start the the AddressBook Web application. Tomcat will automatically call AddressBook.ContextListener.contextInitialized and the entire process will repeat.
When developing a Web application, the cycle goes as follows: make changes
to your software; issue ant install to build and install the Web
application; point your browser at http://localhost:8080/applicationName (or simply hit the browser's refresh button) to test the Web application; issue ant remove to remove the Web application's
context; repeat. When you are happy with the application, issue ant
deploy to permanently deploy the Web application. Now your Web
application will be available after you restart Tomcat, or even after you
reboot your server and start Tomcat. Issue ant undeploy to
permanently undo deployment of the Web application.
Tomcat Manager
Instead of Ant, you can use the Tomcat manager Web page. Point your browser
to http://localhost:8080/manager/html/list. You can see Tomcat's Manager Web page in Figure 6.

Alternatively, you can issue these URLs directly:
http://localhost:8080/manager/listhttp://localhost:8080/manager/resourceshttp://localhost:8080/manager/roleshttp://localhost:8080/manager/start?path=/AddressBookhttp://localhost:8080/manager/stop?path=/AddressBookhttp://localhost:8080/manager/remove?path=/AddressBookhttp://localhost:8080/manager/sessions?path=/AddressBook