How to compile and run your servlets

General information

Servlets are Java programs. As for a regular Java program, the source code for a servlet class MyServlet is in a file When a servlet is compiled, the result of the complilation is a file MyServlet.class.

The servlets run by a program called TOMCAT. TOMCAT runs on and uses the port 8080. Servlets are orgainzed into web applications. A web application may include Java classes, Java .jar files, images, and so on. To access a servlet, one needs to specify the application to which the servlet belongs. For instance, if you type
in your browser, this you are accessing a servlet MySession1 in the web application examples.

When a request for a servlet is made, TOMCAT checks if the servlet with this class is already running. If the servlet is already running and its class has not changed since the last access, then the request is forwarded to the servlet. If its class has changed or the servlet is not running, then its class is started.

The structure of a web application.

A web application is a directory which includes several subdirectories with standard names. The ones that we will be using are: Note that subdirectory servlets in a web application directory has nothing to do with the classes of servlets. You will not need to access it.

A web application with your project login name has been created for each project group. The directory is /etc/tomcat3/conf/jakarta-tomcat-3.3a/webapps/myproj on Instead of myproj you will use your project login name.

How to write and compile your servlet

Suppose you are writing a servlet MyGreatServlet. You write the Java code for the servlet in the file in your account on puma. If it's using other java classes, write each public class in its own file, for instance

To compile the servlet, type

If it uses other classes, you can compile them all together as

or separately. Files MyGreatServlet.class and MyHelperClass.class will be created if the compilation is successful.

Servlets are supported in Java 2 Enterprise Edition (J2EE). So far you have been working with Java 2 Standard Edition. javacee is a variation of the Java compiler javac which includes J2EE libraries. Note that the abbreviation javacee exists only on puma (including puma hosts, s.a. lion, tiger, etc.) and on birch, but not on other Wellesley machines and not outside of Wellesley.

How to run your servlet

After the servlet has successfully compiled, you need to copy it to your web application to run.

To "run" your servlet, you need to copy its class(es) to the directory /etc/tomcat3/conf/jakarta-tomcat-3.3a/webapps/myproj/WEB-INF/classes on birch. Actually, your puma account is accessible on birch with the same password as on puma. Files are shared between puma and birch, so all your file changes on puma will immediately take affect on birch, too.

To copy the class files, you need to:

  1. Connect to birch using ssh. For instance:
    ssh birch
    will connect you to birch with your current login name. You may get the following warning:
    The authenticity of host 'birch (' can't be established.
    RSA key fingerprint is ...
    Are you sure you want to continue connecting (yes/no)?
    Type yes (all three letters).
  2. You will be prompted for your password. After you type it in, you will be logged in to your home directory on birch. If needed, change the directory to the one where the class files are.
  3. Copy the files by the following command:
    cp MyGreatServlet.class /etc/tomcat3/conf/jakarta-tomcat-3.3a/webapps/myproj/WEB-INF/classes/.
    Instead of MyGreatServlet.class use the name of your servlet, and instead of myproj use the login name of your project.
  4. Type in
    in your browser (you may omit to test your servlet. Copy other classes used by the servlet if they have been recompiled.
  5. If your servlet is already running, you need to reload it to test it. Reloading may cause an error message if a form parameter has changed. If this happens, reload the servlet again.
If you are working on a servlet, you might want to keep the ssh connection open while you are working so that you don't need to log in every time you need to copy a file.
This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at

Spring Semester 2002