The server startup script automatically sets the
server's CLASSPATH to include the
standard servlet and JSP classes and the
WEB-INF/classes directory (containing
compiled servlets) of each Web application. But you
need similar settings, or you will
be unable to compile servlets in the first place.
Configuring your server for servlet development involves
the following four steps:
- Creating a development directory
- Making shortcuts to the Tomcat startup and
shutdown scripts
- Setting your
CLASSPATH
- Bookmarking the servlet & JSP javadocs
Details on each step are given below.
The first thing you should do is create a directory in which to place the servlets and
JSP pages that you develop. This directory can be in your home directory (e.g.,
C:\Documents and Settings\Your Name\My Documents\ServletDevel
on Windows 2000) or in a convenient general location
(e.g., C:\ServletDevel). It should not, however,
be in the Tomcat's deployment directory (e.g., anywhere
within install_dir/webapps).
Eventually, you will organize this development directory into
different Web applications
(each with a common structure--see More
Servlets and JavaServer Pages Chapter 4). For initial testing of your
environment, however, you can just put servlets either directly in the development
directory (for packageless servlets) or in a subdirectory
that matches the servlet package
name. Many developers simply put all their code in the server's deployment
directory (within install_dir/webapps).
I strongly discourage this practice and instead recommend
one of the approaches described in the
deployment section.
Although developing in the deployment directory seems simpler at
the beginning since it requires no copying of files,
it significantly complicates matters
in the long run. Mixing locations makes it hard to separate an operational version
from a version you are testing, makes it difficult to test on multiple servers, and
makes organization much more complicated. Besides, your desktop is almost certainly
not the final deployment server, so you'll eventually have to develop a good system
for deploying anyhow.
Since I find myself frequently restarting the server, I find it convenient to place
shortcuts to the server startup and shutdown scripts inside my main development
directory or on my desktop. You will likely find it convenient to do the same.
For example, one way to make these shortcuts is to go to
install_dir/bin, right-click on startup.bat,
and select Copy. Then go to your development directory, right-click in the
window, and select Paste Shortcut (not just Paste). Repeat the process for
install_dir/bin/shutdown.bat. On Unix, you would use
ln -s to make a symbolic link
to startup.sh, tomcat.sh
(needed even though you don't directly invoke this file), and
shutdown.sh.
Since servlets and JSP are not part of the Java 2
platform, standard edition, you have
to identify the servlet classes to the compiler.
The server already knows about the
servlet classes, but the compiler (i.e., javac)
you use for development probably
doesn't. So, if you don't set your CLASSPATH,
attempts to compile servlets, tag libraries,
or other classes that use the servlet API will fail with error messages about
unknown classes. The exact location of the servlet JAR file varies from server to
server, but with Tomcat it is install_dir/common/lib/servlet.jar.
Now, in addition to the servlet JAR file, you also need to put your development
directory in the CLASSPATH. Although this is
not necessary for simple packageless
servlets, once you gain experience you will almost certainly use packages. Compiling
a file that is in a package and that uses another class
in the same package requires the
CLASSPATH to include the directory that is at the
top of the package hierarchy. In
this case, that's the development directory I just discussed.
Forgetting this setting is perhaps the most common
mistake made by beginning servlet programmers.
Finally, you should include "." (the current directory)
in the CLASSPATH. Otherwise,
you will only be able to compile packageless classes that are in the top-level
development directory.
Here are two representative methods of setting the
CLASSPATH. They assume
that your development directory is C:\ServletDevel.
Replace install_dir with the actual base installation
location of the server. Also, be sure to use the appropriate
case for the filenames. Note
that these examples represent only one approach for
setting the CLASSPATH. Many
Java integrated development environments have a global or project-specific setting
that accomplishes the same result. But these settings are totally IDE-specific and
won't be discussed here.
Windows 98/Me
Put the following in your autoexec.bat. (Note that
this all goes on one line with no spaces--it is broken here for
readability.)
set CLASSPATH=.;
C:\ServletDevel;
install_dir\common\lib\servlet.jar
Windows NT/2000/XP
Go to the Start menu and select Settings, then
Control Panel, then System, then Environment. Then, enter the
CLASSPATH value from the previous bullet.
Just as no serious programmer should develop general-purpose Java applications
without access to the JDK 1.3 or 1.4 API documentation (in Javadoc format), no serious
programmer should develop servlets or JSP pages without access to the API for
classes in the javax.servlet packages.
So, open install_dir/webapps/tomcat-docs/servletapi/index.html
in your browser and then add it to your bookmarks (Netscape) or
favorites (Internet Explorer) list. If Tomcat is running, you
can also access the API with
http://localhost/tomcat-docs/servletapi/index.html.
However, you almost certainly will want access to the API even
when the server is not running, so I recommend you open
the file directly from disk and bookmark that location.
Reprinted with permission from
Marty Hall. This tutorial is also available at
http://www.moreservlets.com/Using-Tomcat-4.html
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|