In our last month’s article titled Web Applications with Tomcat 4 (page 75, PCQuest, November 2002) we saw how to write, set up and deploy a simple guestbook Web application. Once we finished writing the Web application, we modified server.xml file and restarted Tomcat(using shutdown.sh and startup.sh scripts) to make it aware of the new Web application.
This time we’ll talk about a Tomcat Web application called ‘manager’ and Web Application Archives. Using manager, you can install and uninstall Web applications on the fly. Web Application Archives allow easy distribution of Web applications.
Does this mean that whenever you wish to deploy new Web applications on Tomcat, we need to restart it for each Web application? As obvious, during the restart process other Web applications running on Tomcat will become unavailable, which is something not desired for a busy site.
To circumvent this issue, the manager Web application lets you deploy new Web applications without restarting Tomcat. Let’snow see how to deploy the guestbook Web application using manager. If you are already running the guestbook Web application, you must remove it before proceeding further. For this, remove the following line
from server.xml and restart Tomcat as:
Before using the manager Web application, you must set up a username and password for it. Open the file tomcat-users.xml found in the directory /opt/tomcat/conf with a Linux text editor. Between the
Substitute shekhar and secret with your username and password, respectively. Save the file, and restart Tomcat.
As per the last month’s article, the files for guestbook Web application will be in the directory /opt/tomcat/webapps/guestbook. For your convenience, we have provided the guestbook web application—with all the code, compiled Servlet and the JDBC drivers—on this month’s CD. Now to deploy the guestbook Web application, using the manager Web application, type in the following URL in your Web browser (Konqueror or Netscape).
When prompted for the username and password, fill in the name and password that you have specified in tomcat-users.xml (shekhar and secret, in our case). If things have gone okay, you will be shown a message like the one below.
OK - Installed application at context path /guestbook
Once installed, you can access the guestbook Web application through the following URL.
Through manager, you can also get a listing of the running web applications as:
Amongst the listed Web applications you should be able to see the guestbook Web application. To uninstall guestbook Web application, key in the following URL in your browser.
Web Application Archive
Most desktop applications combine all the files of an application into a single archive (usually a zip archive) and then distribute it. Similarly, the files belonging to a Web application can be combined into an archive called Web Application Archive. The Web application archives conventionally have a .war extension. To create an archive of the guestbook Web application, change to the directory /opt/tomcat/webapps/guestbook and issue the following command.
/usr/java/j2sdk1.4.0/bin/jar cvf guestbook.war .
Note the dot (.) towards the end, which means to archive all files and subdirectories in the guestbook directory. Suppose you want to install this Web application on Tomcat running on another Linux machine (call it B), copy guestbook.war file to any directory, say /opt, on machine B. The manager Web application can also install archived web applications. Hence, with Tomcat running on machine B, key in the following URL in the Web browser on the same machine.
Check out the successful installation using the following URL.
Though Tomcat’s manager Web app is very useful in installing and uninstalling Web applications without restarting the Tomcat server, there is a catch in it. The Web applications deployed with manager are volatile. The moment you restart Tomcat, all the Web applications loaded through manager will get removed until you again install them using manager. This is because manager does not make entries in the server.xml file, which is read by Tomcat (each time Tomcat is started) to get information about the Web applications.
Hence, whenever you deploy a Web application using manager, manually add the line describing the Web application in the server.xml file. That is, for the guestbook Web application add the following line
in server.xml file (as we did in the last month’s article).
In the Archive’s example, where we packaged the Web application into an archive and kept it under the /opt directory, modify the description line as follows.
The docBase attribute specifies the location of the Web application on the hard disk. In case the preceding / is omitted, Tomcat assumes that the Web application is located relative to the directory webapps.
For more on the manager Web application refer to http://jakarta.apache.org/tomcat/tomcat-4.0-doc/manager-howto.html