Pieter J. Berkel's Web Site
Home > Teaching & Courses > Applied Internet Technology
Home Page
Photo Gallery
Travel Journal
My Teaching
 - Applied Internet Tech
 - Learn Pascal
My Resume
Contact Details
Installing/Compiling Servlets
Home > Installing/Compiling Servlets Home |  Lectures |  Extras |  Links
Summary
This Handout summarizes the steps necessary for installing and compiling your own servlets. Before you begin, please read through the entire handout.

Downloading and Installing the Necessary Software
In order to edit, compile and run your Java Servlets, you will need three pieces of software. First, you need an editor for typing your Java programs. This course recommends the use of the TextPad editor. Second, you need a copy of the Java 2 Software Development Kit. This kit includes everything you need to compile Java applets and applications. Third, you need a copy of the Java Servlet Development Kit. This Kit includes the Java Servlet API and a free utility, Servlet Runner for running your servlets. Please follow the steps outlined below.

1. Download the TextPad Editor (2.3 Mbytes)

The TextPad Editor is available for free at: http://www.textpad.com/download/index.html

2. Download the Java 2 Software Development Kit, Version 1.3 (30 Mbytes)

The kit is available at: http://java.sun.com/j2se/1.3/download-windows.html.

By default, the JDK will be installed to: c:\jdk1.2

3. Download the Java Servlet Development Kit 2.1: (375 KB)

The Servlet Development Kit is available at: http://java.sun.com/products/servlet/download.html.

By default, the Servlet Development Kit will be installed to: c:\jsdk2.1

Running the Java Servlet Runner
Once you have downloaded and installed all the necessary software, you are ready to run the Java Servlet Runner and run the sample Servlets provided.

1. Starting Servlet Runner

  1. Open an MS-DOS Window
  2. Go to the JSDK2.1 root directory:   CD c:\jsdk2.1
  3. Run the startserver command:   startserver
    By default, Servlet Runner will run on Port 8080.
  4. Open your web browser and go to: http://localhost:8080/
  5. In your browser, you should see an index page of Sample servlets. Click any one of the "Execute" links to run the servlet.

2. Stopping Servlet Runner

  1. To stop Servlet Runner, run the stopserver command:   stopserver

Setting up your Environment
Before you compile your own servlets, you may choose to set your PATH variable. Information on setting the PATH variable is available below. Note that the steps are different depending on whether you have Windows 95, 2000 or Windows NT.

Excerpted From the Sun Java Installation Directions:

[Full Instructions from Sun]

Updating the PATH variable

You can run the Java 2 SDK just fine without setting the PATH variable, or you can optionally set it as a convenience.

Should I set the PATH variable?

Set the PATH variable if you want to be able to conveniently run the SDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:> \jdk1.3\bin\javac MyClass.java

It's useful to set the PATH permanently so it will persist after rebooting.

How do I set the PATH permanently?

To set the PATH permanently, add the full path of the jdk1.3\bin directory to the PATH variable. Typically this full path looks something like C:\jdk1.3\bin. Set the PATH as follows, according to whether you are on Windows NT or Windows 95/98/2000.

Windows NT and Windows 2000 - To set the PATH permanently:

Start the Control Panel, select System, select Environment, and look for "Path" in the User Variables and System Variables. If you're not sure where to add the path, add it to the right end of the "Path" in the User Variables. A typical value for PATH is: C:\jdk1.3\bin

Capitalization doesn't matter. Click "Set", "OK" or "Apply". The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to jdk1.3.

The new path takes effect in each new Command Prompt window you open after setting the PATH variable.

Windows 98, Windows 95 - To set the PATH permanently

Open the AUTOEXEC.BAT file and add or change the PATH statement as follows:

Start the system editor. Choose "Start", "Run" and enter sysedit, then click OK. The system editor starts up with several windows showing. Go to the window that is displaying AUTOEXEC.BAT.

Look for the PATH statement. (If you don't have one, add one.) If you're not sure where to add the path, add it to the right end of the PATH. For example, in the following PATH statement, we have added the bin directory at the right end:

PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\JDK1.3\BIN

Capitalization doesn't matter. The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows searches for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to jdk1.3.

To make the path take effect in the current Command Prompt window, execute the following: C:> c:\autoexec.bat

To find out the current value of your PATH, to see if it took effect, at the command prompt, type: C:> path

Compiling Your Servlets

1. Finding the Right Directory

By default, Servlets are located in: C:\jsdk2.1\examples\WEB-INF\servlets.

First, confirm that this directory exists. It should contain several sample servlets, including: HelloWorldExample.java, CookieExample.java, etc.

It is recommended that you place all your servlets within this directory.

2. Compiling your Servlets

To compile, your servlets, you first need to cd to the proper directory. For example:

cd d:\jsdk2.1\examples\WEB-INF\servlets

You then need to run the javac command. In order to compile properly, however, you must include the servlet.jar file in your CLASSPATH. You can do this via the -classpath option. For example:

javac -classpath c:\jsdk2.1\servlet.jar HelloWorldExample.java

If you don't want to specify the CLASSPATH variable each time, you can specify it as an environment variable (see step 4 below.)

3. Restarting ServletRunner

Each time you recompile your servlet, you must restart Servlet Runner. See the steps outlined above.

4. (Optional) Updating the CLASSPATH variable

Excerpted From the Sun Java Installation Directions:

[Full Instructions from Sun]

The CLASSPATH variable is one way to tell applications written in the Java programming language (including the SDK tools) where to look for user classes. (The -classpath command-line switch is the preferred way.) If your machine does not have the CLASSPATH variable set, you can ignore the rest of this step. To check this, run the set command from the DOS prompt: C:> set

If CLASSPATH does not appear in the list of settings, it is not set. If your CLASSPATH variable is set to some value, you may want to clean up your CLASSPATH settings, so read on.

How do I modify the CLASSPATH?

Use the same procedure you used for the PATH variable in the previous step and either:

Remove the CLASSPATH environment variable entirely. With Java 2 SDK, the default value is ".", the current directory. To include any user classes, use the -classpath command line switch instead with java, javac, javadoc and other tools. This is the recommended approach because it doesn't force one CLASSPATH for all applications.

-OR-

If you have applications that require CLASSPATH be set, keep those required user classes in CLASSPATH and include the current directory "." If you're no longer using JDK 1.1.x, remove classes.zip.

Copyright © 2002-2012 Pieter J. Berkel. All rights reserved.