Compiling servlets, situating them within a Java Server context, and running them is not as straight-forward as working with Java applets or applications. One gets the impression that the technology is not yet mature.
Below is a list of the steps that are necessary for constructing and using servlets. The specifics refer to the UNC Department of Computer Science environment, but the general process should hold for other systems.
1. Set the CLASSPATH.
Because the Java classes that define the servlet API are not part of the core Java framework, but rather the Standard Java Extension API, they must be included in the classpath so that references to them can be resolved by the Java compiler. One source is the lib directory that comes with Sun's Java Web Server. The path to that directory is the following:
/afs/unc/pkg/JavaServer/JavaWebServer1.0.3/lib/classes.jar
You need to add that directory to your classpath if you are working within UNIX. If you are working on a PC, you must locate this directory within the local file system and add it. If you are using Cafe, you may add the classpath to the project for the servlet by editing its properties.
2. Write and compile the servlet.
Once the classpath is set, writing the Java code for the servlet and compiling it is straight-forward. You can do this "manually" using JDK or you can use an environment, such as Cafe. If you use Cafe, you will be able to compile the program, but you cannot test it there. It has to be tested using a Java Server. If you are using the wwwj server, be especially careful to insure that your servlets terminate properly.
3. Copy the servlet to the appropriate directory.
Sun's Java Web Server is very restrictive with respect to the locations where it expects to find servlets. it provides three options. One is a directory, called servlets, included in the root directory of the server, itself. A second option is a directory included in the CLASSPATH of the server, itself, set by the server administrator when the server is started. A third option is an arbitrary directory located on the Internet. However, servlets loaded from remote locations must be signed by the author and the Java Server security restrictions must be set accordingly.
What one would like is subdirectories for different servlet authors . I have not found any reasonable way to accomplish this. Consequently, we will use the second option -- a servlets directory that is included in the CLASSPATH of the server. Specifically, there is a servlets directory in the root directory for the course. Place your servlets there. Since all of our servlets will be in a single directory, prepend your login to the name of each of your servlets. For example, my servlets will all be of the form jbsServletName
4. Invoke the servlet.
The Java Server actually invokes a servlet, analogous to the appletviewer, to provide a context in which your servlet runs. This process is specified as part of the server administration process. Specifically, the server is configured so that it interprets the keyword, servlet, within a URL as calling for the invoker process, and the name that follows is interpreted as the name of the servlet to be run by the invoker. The server looks for the servlet to run within its servlets directory and, if it doesn't find it there, uses its CLASSPATH to look for it in the directories designated there. Thus, it will eventually find your servlets in the course servlets directory.