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:

The primary difference between servlets and CGI scripts is that:

What this means

  1. Servlets almost always use less memory than CGI scripts and execute faster.
  2. 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).

  3. 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).
  4. 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.
  5. 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.
  1. 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.
  2. 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.
  3. 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.

  4. Restart the web server (this can be accomplished in severl different ways).
  5. 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