Servlets
Servlets are the server analogue of applets: just as applets run on a
web browser, servlets run on the server-host. Both are written in Java,
as well.
Servlets perform roughly the same function as CGI-scripts:
- They parse form-data and generate a web page reply.
- They access services on the host machine (databases, etc.)
The primary difference between servlets and CGI scripts is that:
- (Usually) only a single copy of a servlet is present in memory
and it handles all requests. It is called in a "reentrant" manner
by the supervisory program (a Java program run as a task), which creates
lightweight threads to handle client requests. Although the supervisory
program automatically creates threads for each client request, all of
these threads call the same servlet code.
- CGI scripts are run by heavyweight processes spawned by the web server.
What this means
- Servlets almost always
use less memory than CGI scripts and execute faster.
- Care must be taken in writing servlet code to make it
thread-safe (or reentrant). If the servlet has no local state, this presents
no problem. If it uses local variables and stores information about the
client, one must use process synchronization primitives (the
Servlet Development Kit contains routines to facilitate this) to prevent race
conditions and corruption of data.
In cases where this is impossible,
one can specify that the servlet implements SingleServerThread
in which case
the servlet is handled exactly as a CGI script (multiple copies
are spawned for each client request).
- Servlets are persistant: once started,
they continue to run after a given client
is finished
(on some systems, they run until the web server is shut down).
This means that they can store "permanent" state information
--- something CGI scripts cannot do (they must pass state information
as hidden fields on the forms that they generate).
- Servlets that respond to clients are written like CGI scripts:
they parse form-data and generate a reply in exactly
the same way. Since Java doesn't have the built-in
text handling features of Perl, there are routines in
the Servlet Development Kit to facilitate the parsing.
- Servlets can call other servlets (and one can write servlets
that are not meant to be accessed by web browsers at all
--- they are only
meant to be called by other servlets).
Configuring Servlets
Note: these instructions are geared toward Redhat Linux and the Apache
web server.
- Install the latest version of Java.
A good one is highest-numbered version at
www.blackdown.org
Be sure to remove other versions of Java from your system.
- Download version 2.0 of Sun's Servlet Development Kit
for Solaris from
java.sun.com/product/servlet/download
and install the jar file in a place where java can find it.
- Download
java.apache.org/jserv/dist/ApacheJserv-1.1-2_RH6x.i386.rpm
and install it. The install script will display a test servlet
that one can try as a way of knowing whether the install went OK.
This is probably the safest way to automatically
reconfigure Apache.
- Restart the web server (this can be accomplished in severl different
ways).
- Load the test page mentioned above.
Note: If you are not on the Internet (as a more or less permanent host)
there will be problems with this. To solve them, go to the configuration
files in
/etc/httpd/conf
and replace every occurence of 'localhost' by '127.0.0.1'. You have
to go into the jserv directory as well and do this. Then, to access,
a servlet, you must access a page like
http://127.0.0.1/servlets/name
Servlet Links