Java Servlets

Servlets are programs that run within the context of a server, analogous to applets that run within the context of a browser. They can be used to implement an arbitrary service easily and efficiently. Easily, since they the programmer may take advantage of the support provided by the Java Web Server, such as parsing HTTP requests and handling connections to Web clients. Efficiently, since servlets run as separate light weight threads of the Java Server process rather than as separate heavy weight processes; thus, they load and execute much faster than CGI programs.

The basic architecture is shown, below:

  

Clients communicate with a Java server through HTTP, addressing a specific server and a specific servlet through a specific URL.  The portion of the server that supports servlets is often referred to as a servlet engine.  The servlet engine may be all inclusive, supporting both conventional requests for Web pages and servlet processing, or it may be a stand-alone process that works in conjunction with a conventional Web server that passes servlet requests to it.  One popular servlet engine and the one currently used in the UNC Web Programming sequence, is Tomcat.  It can be configured either as a dedicated servlet engine that works with an Apache Web server or as a complete server that provides both functions.  In the diagram, above, it is configured to provide both functions.

Note that servlets can be accessed by any client that can submit an HTTP request and process the results returned.  Since Web browsers provide basic support for HTTP processing, servlet clients may be conveniently situated within browsers.  Consequently, two common forms of clients are Java Applets and HTML Forms.

In the discussion that follow, we will begin by looking at several concepts basic to servlets. We will then look at the mechanics of writing, compiling, and running servlets. 

Additional references you may wish to consult include the Java Servlet api and Sun's Servlet Tutorial.


Basic Concepts

Two basic concepts are discussed: the "rhythm of interaction" that is required for a servlet to be invoked and the class hierarchy for servlets.


Mechanics

The steps necessary to compile, store, and run a servlet are discussed.


Example Servlets