Article
Java Servlets - Part 1
Compiling a Servlet
All of the Java programs we've seen so far were very simple to compile. Unfortunately, if you try to compile the MyServlet.java file we produced in the previous section, you'll get a number of pretty ugly error messages:
D:\javadev> javac MyServlet.java
MyServlet.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
MyServlet.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
MyServlet.java:5: cannot resolve symbol
symbol : class HttpServlet
location: class MyServlet
public class MyServlet extends HttpServlet {
^
...
Now, the best tactic I find when tackling compilation errors is to look at the errors listed at the top first, since those errors could actually be causing some of the other errors that are appearing lower down. In this case, for example, the four "cannot resolve symbol" errors you're likely to see are actually a result of the two "package does not exist" errors at the top. Looking more closely, you'll notice that the compiler is choking on the two import commands that we used to import the javax.servlet and javax.servlet.http packages. Since it can't import those packages, we won't be able to use any of the classes that reside in them, and it turns out that all of the "cannot resolve symbol" errors refer to places where we're trying to do so.
So why can't it find those two packages? Well, these two packages aren't actually built into Java like java.io is. Instead, they come with the Servlet-capable Web server (e.g. Tomcat). So before the Java compiler will be able to compile our Servlet, we need to let it know where to find the classes in these two packages.
The classes required are normally stored in a file called servlet.jar. The exact location of this file will depend on the particular Web server software you use, but in the case of Tomcat you can find it in the lib subdirectory of the main Tomcat installation directory (e.g. d:\Program Files\Apache Group\jakarta-tomcat-3.2.3\lib\servlet.jar). For the Java compiler to be able to compile Servlets, you need to add this file to your Java class path. By default, Java looks for classes in the current directory (".") only. Thus, "." is the default class path. If you change the class path to include the servlet.jar file (".;d:\...\lib\servlet.jar" under Windows, ".:/usr/.../lib/servlet.jar" in Unix), then the Servlet should compile just fine.
You can specify a class path to use when you run javac.exe as follows:
d:\javadev> javac -classpath ".;d:\Program Files\Apache Group\
jakarta-tomcat-3.2.3\lib\servlet.jar" MyServlet.java
Obviously, the path to servlet.jar can be quite long and painful to type every time you want to compile a Servlet. As an alternative, you can set an environment variable called CLASSPATH to your desired class path. Set this the same way you do the PATH environment in the particular operating system you are using (instructions for adjusting the PATH environment variable in various operating systems are provided in the install instructions for the JDK if you need them). To temporarily set the CLASSPATH environment variable under Windows, you can use the SET command on the MS-DOS Command Prompt:
d:\javadev> SET CLASSPATH=.;d:\Program Files\Apache Group\
jakarta-tomcat-3.2.3\lib\servlet.jar
d:\javadev> javac MyServlet.java
Whichever way you go about setting your class path, you should now be able to successfully compile MyServlet.java to obtain MyServlet.class (or download it here).