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


AddThis Social Bookmark Button

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 init initializes the time stamp.

  • ant prepare creates a war directory structure in the AddressBook directory, as follows: a war directory, a war/WEB-INF, a war/WEB-INF/classes directory, and a war/WEB-INF/lib directory.

  • ant build builds the Web Application by copying the jsp files into the war directory, copying the context.xml files into the war/META-INF directory, copying the web.xml file into the war/WEB-INF directory, and compiling any Java files into the war/WEB-INF/classes directory.

  • ant package creates the Web archive file from the war directory. The Web archive file is a .jar file and is created using the jar application.

  • Finally, ant install installs the Web archive file into Tomcat using the war/META-INF/context.xml configuration 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. ant uses the tomcatusername and tomcatpassword specified in AddressBook/build.properties to 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.

Figure 4

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.

Figure 5

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.

Figure 6

Alternatively, you can issue these URLs directly:

Pages: 1, 2, 3, 4

Next Pagearrow