OK, so your environment is all set. At least you
think it is. It would be nice to confirm
that hypothesis. Verifying this involves the
following three steps:
- Testing a packageless servlet
- Testing a servlet that uses packages
- Testing a servlet that uses packages
and utility classes
Details on each step are given below.
The first servlet to try is a basic one:
no packages, no utility (helper) classes, just
simple HTML output. Rather than writing your own test servlet,
you can just download and install
HelloServlet.java.
Right-click on the link to download the file to your system.
If you get compilation errors, go back and check your
CLASSPATH settings (see the earlier section
on this topic)--you most likely erred in listing the location of the
JAR file that contains the servlet classes
(i.e., install_dir/common/lib/servlet.jar).
Once you compile HelloServlet.java, put HelloServlet.class
in install_dir/webapps/ROOT/WEB-INF/classes. After compiling the code,
access the servlet with the URL
http://localhost/servlet/HelloServlet
(or http://localhost:8080/servlet/HelloServlet if you
chose not to change the port number as
described earlier). You should get
a simple HTML page that says "Hello".
If this URL fails but the test of the server itself
succeeded, you probably put the class file in the wrong directory.
The second servlet to try is one that uses packages but no utility classes. Again,
rather than writing your own test, you can download and install
HelloServlet2.java.
Since this servlet is in the moreservlets package, it should go
in the moreservlets directory
both during development and when deployed to the server. If you get compilation
errors, go back and check your CLASSPATH
settings--you most likely forgot to include "." (the current directory).
Once you compile>HelloServlet2.java, put HelloServlet2.class in
install_dir/webapps/ROOT/WEB-INF/classes/moreservlets.
For now, you can simply copy the class file
from the development directory to the deployment directory.
However, an upcoming section
will provide some options for simplifying the deployment process.
Once you have placed the servlet in the proper directory, access it with the URL
http://localhost/servlet/moreservlets.HelloServlet2. You should get
a simple HTML page that says "Hello (2)". If this test fails,
you probably either typed the URL wrong (e.g., used
a slash instead of a dot after the package name) or
put HelloServlet2.class in the
wrong location (e.g., directly in install_dir/webapps/ROOT/WEB-INF/classes
directory instead of in the moreservlets subdirectory).
The final servlet you should test to verify the configuration of
your server and development
environment is one that uses both packages and utility classes.
HelloServlet3.java
is a servlet in the moreservlets package that uses
the
ServletUtilities class to simplify the generation of the
DOCTYPE (specifies the HTML version--useful
when using HTML validators) and HEAD
(specifies the title) portions of the
HTML page. Those two parts of the page are useful (technically required, in fact),
but are tedious to generate with servlet println statements.
Since both the servlet and the utility class are in
the moreservlets package,
they should go in the moreservlets directory.
If you get compilation errors, go back
and check your CLASSPATH
settings--you most likely forgot to include
the top-level development directory. I've said it before, but
I'll say it again:>your
CLASSPATH must include the top-level directory
of your package hierarchy before
you can compile a packaged class that makes use
of another class from the same
package. This requirement is not particular to servlets;
it is the way packages work on
the Java platform in general. Nevertheless,
many servlet developers are unaware of
this fact, and it is one of the (perhaps the) most
common errors beginning developers
encounter.
Once you compile HelloServlet3.java (which will automatically
cause ServletUtilities.java to be compiled), put
HelloServlet3.class and ServletUtilities.class in
install_dir/webapps/ROOT/WEB-INF/classes/moreservlets. Then,
access the servlet with the URL
http://localhost/servlet/moreservlets.HelloServlet3. You
should get a simple HTML page that says "Hello (3)".
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.
|