Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.2 A Simple Servlet Generating Plain Text
25
3. A directory for infrequently changing servlets in JAR files.
With the second option above, the class files are placed directly
in the
classes
directory or in subdirectories corresponding to
their package name. Here, the class files are packaged in a JAR
file, and that file is then placed in the designated directory. With
Tomcat, the JSWDK, the Java Web Server, and most other serv
ers, this directory is
install_dir/lib
. You must restart the
server whenever you change files in this directory.
Once you've configured your server, set your
CLASSPATH
, and placed the
servlet in the proper directory, simply do
javac HelloWorld.java
to
compile the servlet. In production environments, however, servlets are fre
quently placed into packages to avoid name conflicts with servlets written by
other developers. Using packages involves a couple of extra steps that are
covered in Section 2.4 (Packaging Servlets). Also, it is common to use HTML
forms as front ends to servlets (see Chapter 16). To use them, you'll need to
know where to place regular HTML files to make them accessible to the
server. This location varies from server to server, but with the JSWDK and
Tomcat, you place an HTML file in
install_dir/webpages/path/file.html
and then access it via
http://localhost/path/file.html
(replace
localhost
with the real
hostname if running remotely). A JSP page can be installed anywhere that a
normal HTML page can be.
Invoking the Servlet
With different servers, servlet classes can be placed in a variety of different
locations, and there is little standardization among servers. To invoke servlets,
however, there is a common convention: use a URL of the form
http://host/servlet/ServletName
. Note that the URL refers to
serv
let
, singular, even if the real directory containing the servlet code is called
servlets
, plural, or has an unrelated name like
classes
or
lib
.
Figure 2 1, shown earlier in this section, gives an example with the Web
server running directly on my PC ( localhost means the current machine ).
Most servers also let you register names for servlets, so that a servlet can
be invoked via
http://host/any path/any file
. The process for doing
this is server specific; check your server's documentation for details.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved