Getting Started with Servlets

For your second project assignment, you will be writing Java Servlets. This document should help you get started with that process.

Writing and Compiling Your Code

You can write and compile your code in just about any environment you like. However, for this assignment you should use a recent version of java (1.4.x) and make sure to have the appropriate jar files in your classpath.

Compiling in Clapp 202 Using JBuilder: You can use JBuilder to write and compile your code. However, you must still transfer your .class files to napa in order to perform testing. Instructions for using JBuilder:
  1. You will need to register to use JBuilder before you can launch it from the computers in the lab. The following web page has more information on this step: http://www.mtholyoke.edu/courses/srollins/cs325-f03/web/jbinlab.html
  2. Create a new Project and write your code.
  3. In order to compile, you need to let JBuilder know where to find the jar file which contains the necessary Servlet classes. There should be a copy of servlet-api.jar in the directory:
    c:\tools\servlets\
    If not, you can download the file here:servlet-api.jar and save it to c:\tools\servlets\servlet-api.jar. To point JBuilder to this jar, select Project -> Project Name Properties. From this window, select the Classpath tab. Select New and enter the path (c:\tools\servlets\servlet-api.jar). Click OK. This should tell JBuilder where to find the jar. Now, you should be able to compile and fix any errors in your code.
  4. Now, you will need to transfer your .class files to /home/username/public_html/WEB-INF/classes on napa. To do this, you can use WinSCP. There should be a WinSCP icon on the desktop. If not, it should be installed in the c:\tools directory. After you transfer your files, make sure the permissions are set to read by all.
Compiling in Clapp 202 Using the Command Line:
  1. You will need to set your PATH to point to the location of javac.exe and java.exe. In the lab, java should be located in the directory c:\JBuilder9\jdk1.4\bin. To set your PATH, type:
    set PATH=c:\JBuilder9\jdk1.4\bin;%PATH%
  2. When you compile, you will need to specify the location of servlet-api.jar. To do this, you will compile using a command such as the following:
    javac -classpath c:\tools\servlets\servlet-api.jar FileName.java
  3. Once you have successfully compiled your code, you will need to transfer your class files to /home/username/public_html/WEB-INF/classes on napa.
Compiling on napa: If you like, you can write and compile your code on napa directly. You can log in remotely and use emacs or vi to write your code.
  1. In order to compile on napa, you will need to put java in your PATH. In order to do this, type:
    export PATH=/local/apps/j2sdk/bin/:$PATH
    You should now be able to use the commands javac and java. However, you also need to make sure that servlet-api.jar and mysql.jar are in your classpath. You can download mysql.jar here: mysql.jar. In order to set your classpath, type:
    export CLASSPATH=.:{path}/servlet-api.jar:{path}/mysql.jar

Testing Your Servlets

  1. In your home directory on napa (/home/username), create a directory called public_html and set the permissions to allow read and execute by all.
  2. In public_html, create a directory called WEB-INF and set the permissions the same as above.
  3. In WEB-INF, create a directory called classes and set the permissions the same as above.
  4. Save the file web.xml in the WEB-INF directory and make sure that the permissions are set as read by all.
  5. Upload all of your .class files into $HOME/public_html/WEB-INF/classes and make sure the permissions are set to read and execute for all.
  6. Open a web browser and go to:
    https://napa.mtholyoke.edu:8443/~yourUserName/servlet/NameOfServlet

Sami Rollins