Developing Applications with Tomcat
The Actual Development Process
The following sections highlight the commands and tasks that you, as the
developer of the code, will perform. The same basic approach works when you
have multiple programmers involved, as long as you have an appropriate source
code control system and internal team rules about who is working on what parts
of the application at any given time.
Create Project Source Directory
The first step is to create a new project source directory,
and customize the build.xml and build script you will be using.
The directory structure is described in the section labeled Source Organization,
or you can use the sample application located in doc/appdev as a starting point.
Configure Tomcat to Recognize Your Application
In order for Tomcat to recognize your application,
you must integrate it as described in the section labeled Integration with Tomcat.
Any of the proposed techniques can be used. For our purposes,
we will assume that you are using the first approach (unpacked hierarchy),
because we set the deployment home to be an appropriate directory under
the %TOMCAT_HOME%/webapps directory.
Editing the Source and Building the Web Application
One may make changes to any of the source files (java, html, scripts, etc)
and once saved we are ready to build our web application. We compile the
application by issuing the build command from the project source root directory.
The Ant tool will be utilized to compile any new or updated Java code.
We do this once we have ensured that the following variables have been set:
- The JAVA_HOME points at the base directory where you have installed the JDK (for example, c:\jdk1.2.2).
- The TOMCAT_HOME points at the base directory where you have installed Tomcat (for example, c:\jakarta-tomcat).
And the following have been done.
- You have added directory %JAVA_HOME%/bin to your PATH environment variable, so that the java command is recognized and executed.
- Normally, any changes required to the CLASSPATH environment variable are handled for you by the development scripts. However, if you are defining your own scripts, you may need to add file %JAVA_HOME%/lib/tools.jar to your CLASSPATH.
Test Your Web Application
To test your application, you will want to execute it under Tomcat. Assuming you have integrated your application as described earlier, this is very simple. Under Windows, execute:
%TOMCAT_HOME%\bin\startup
This command starts Tomcat as a background process. Now, point your web browser at the home page for your application, by opening the following URL (where "/myapp" is the context path you have assigned to it):
http://localhost:8080/myapp
Now, you can test your application to verify that it operates correctly. When you discover something that needs to change, fix it as follows:
- To change a JSP page, modify it in the source directory and then
re-execute the build script. The updated page will be recopied, and Tomcat
will recognize this the next time that page is accessed -- the page will
then be recompiled automatically.
- Changing a servlet or other Java class is similar, but the effort
required depends on whether you selected the "autoreload" attribute for
this context when you integrated with Tomcat. First, edit the file in its
source directory, and re-execute the build script. The updated Java class
will be recompiled. If autoreloading is selected, Tomcat will notice this
change the next time this class is referenced, and will automatically unload
and reload your application. Otherwise, you will need to manually stop and
restart Tomcat before continuing.
Do not forget to commit your changes to the source code repository when you
have completed your testing!
Deploy Your Web Application
When you are through adding new functionality, and you've tested everything,
it is time to create the distributable version of your web application that
can be deployed on the production server.
The following general steps are required:
- Issue the command build all from the project source directory, to rebuild everything from scratch one last time.
- Issue the command build dist to create a distributable web application archive (WAR) file, as well as a JAR file containing the corresponding source code.
- Give the WAR file to the system administrator of your production server environment, so that he or she can install it.
NB. The batch files found in %TOMCAT_HOME%\bin have been written and tested under Windows NT.
Some modifications may be necessary for operation on a Windows 98 platform.