A servlet is a unit/sig that imports the servlet^ signature and exports nothing. (Search in help-desk for more information on unit/sig and on signatures.) To construct a unit/sig with the appropriate imports, the servlet must require the two modules providingunit/sigs and the servlet^ signature:

(require (lib "unitsig.ss")
         (lib "servlet-sig.ss" "web-server"))
(unit/sig ()
  (import servlet^)
  ...insert servlet code here...)

The last value in the unit/sig must be a response to an HTTP request.

A Response is one of the following:

Evaluating (require (lib "servlet-sig.ss" "web-server")) loads the servlet^ signature consisting of the following imports:

The path part of the URL suplies the file path to the servlet relative to the "servlets" directory. However, paths may also contain extra path components that servlets may use as additional input. For example all of the following URLs refer to the same servlet:

The above imports support handling a single input from a Web form. To ease the development of more interactive servlets, the servlet^ signature also provides the following functions:

The argument, a function that consumes a string, is given a URL that can be used in the document. The argument function must produce a response corresponding to the document's body. Requests to the given URL resume the computation at the point send/suspend was invoked. Thus, the argument function normally produces an HTML form with the "action" attribute set to the provided URL. The result of send/suspend represents the next request.

The servlet-helpers module, required with

(require (lib "servlet-helpers.ss" "web-server"))
provides a few additional functions helpful for constructing servlets:

Special URLs

The Web server caches passwords and servlets for performance reasons. Requesting the URL

http://my-host/conf/refresh-passwords
reloads the password file. After updating a servlet, loading the URL
http://my-host/conf/refresh-servlets
causes the server to reload each servlet on the next invocation. This loses any per-servlet state (not per servlet instance state) computed before the unit invocation.

Examples of Servlets

Powered by