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 -- specifically, the Tomcat Java Server that runs on port 8888 -- 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. A source for the servlet package that comes with Tomcat is the common/lib directory of the installed server. The name of the jar file is servlet-api.jar.
You need to add this servlet-api.jar file to your local classpath.
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 a development environment. It has to be tested using a Java Server you have installed in your development environment.
3. Deploy the servlet locally.
Java Web Servers are very restrictive with respect to the locations where they expect to find servlets. Tomcat provides three options. One is a special directory designated by the server. For Tomcat, that directory is WEB-INF/classes which is located beneath its webapps directory..
A second option is to register individual servlets by name with the server. Doing so is not practical for a class this size.
A third option, discussed earlier with repsect to setting up your local environment, is to create a /members/your_login/WEB-INF/classes set of directories and place your servlet .class files in the classes directory. This, of course, requires that you include a context that maps /your_login to that the your_login directory under members.You should use this third option for your development
4. Invoke the servlet locally.
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. We have configured the server to look in your individual course directories for your servlets.
The general form of the URL you will use is the following:
http://your_host:8888/your_login/servlet/your_servletwhere your_host is references your computer. It can be either its actual name or localhost. your_login and your_servlet are specific to you and to your particular program.
5. Deploy the servlet.
We have built for you a directory structure in your individual course directory in which you should put both your source files and your class files. There is a WEB-INF/classes directory where you should put all of your servlets .class files. Place all of your servlet .java files in your protected source directory.
6. Invoke the servlet.
The Java Server used for course work is wwwj.cs.unc.edu. Thus, the general form of the URL you will use is the following:
http://wwwj.cs.unc.edu:8888/your_login/servlet/your_servletwhere your_login and your_servlet are specific to you and to your particular program.
An example that goes to my course directory is the following:
http://wwwj.cs.unc.edu:8888/jbs/servlet/jbsGetPostEcho.