Java Reference
In-Depth Information
Compiling Servlets
Your servlet container should come with a JAR file that contains the class files for
the Servlet API. It will probably be in a file named lib/servlets.jar . You need this
JAR file to be able to compile the classes in this chapter. Either edit your CLASS-
PATH environment variable to include the file or place a copy of the file in the lib/
jr e/ext/ directory of your Java SDK, where the Java VM can find it automatically.
Once you have done this, you can compile servlet classes with javac , just as you
would compile other Java classes. If you cannot find the Servlet API classes in
your servlet container distribution, you should be able to download them from
Sun's web site ( http://java.sun.com/pr oducts/servlet/ ). Be careful to download a
version that matches the specifications supported by your servlet container.
Installing and Running Servlets
Once you've compiled a servlet, you need to place the resulting class file (or files)
where the web server can find them. In general, this is a server-specific deploy-
ment task, so you should read the documentation that comes with your servlet
container to figure out how to do it.
Fortunately, however, Version 2.2 of the Servlet API has introduced the concept of
a web application —a set of one or more cooperating servlets, along with all the
auxiliary files they require—and standardized the techniques for deploying the
servlets that comprise a web application. Every web application includes a file
named WEB-INF/web.xml that provides all the necessary servlet deployment
details. Web applications may be packaged into WAR archives using the JAR
archive format. We'll discuss WAR archives and the web.xml file at the end of this
chapter.
The online version of the examples for this chapter includes a prepackaged WAR
file. If you are using Tomcat or another 2.2-compliant servlet container, you can
deploy all the examples in this chapter simply by placing the WAR file at the loca-
tion specified by your container's documentation. For Tomcat 3.1, you simply drop
the WAR file into the webapps/ directory and restart Tomcat. (When Tomcat 3.1
encounters a new web application deployed in a WAR archive, the first thing it
does is extract all the files from the archive and store them in a subdirectory of the
webapps/ directory. This is convenient because it allows you to start with the
prepackaged javaexamples2.war archive, and easily modify, recompile, and rede-
ploy the examples without recreating the WAR file.)
With WAR archives, the name of the WAR file is used as a URL prefix for all the
servlets and other files in the archive. For example, suppose you deploy the
javaexamples2.war archive in Tomcat's webapps/ directory. If the WAR archive
contains a file named index.html at the root level, the URL for this file is:
http://localhost:8080/javaexamples2/index.html
Servlet URLs are similar. The web.xml file defines a mapping of servlet names to
the names of the classes that implement them. For example, the web.xml file in
javaexamples2.war contains a mapping from the name hello to the class
Search WWH ::




Custom Search