s c h e m a t i c s : c o o k b o o k

/ WebProgramming? / Cookbook.WebCGIProgramming

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

CGI programming

Problem

You want to write scripts that follow the Common Gateway Interface, aka CGI, for instance to request information to register new users to your site.

Solution

There are two major components in writing scripts for the Web, the first one is to be able to write the HTML that will initiate the execution of the script, and the other is to write the program that will process the information received through the HTTP protocol.

HTTP forms

We will presume that you know the basics, so here's a very simple example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head><title>Registration</title></head>
 <body>
  <h1>Registration</h1>
  <p>To register as a new user, simply fill out this form:</p>

  <form action="/servlets/doregister.ss" method="post">
   <table border="0">
    <tr><td align="right"> First & last name: </td>
        <td><input type="text" name="Name" size="40" value=""/></td></tr>
    <tr><td align="right"> Email address: </td>
        <td><input type="text" name="Email" size="40" value="" /></td></tr>
    <tr><td colspan="2" align="center"> <input type="submit" value=" Submit " /></td></tr>
   </table>
  </form>
 </body>
</html>

This HTML form has two important bits of information, on the one hand the method used to send the information from the browser to the server, in this case post, which packs the information and sends it to the server once you hit the Submit button.

On the other hand, we have the action, in this case an scheme program that will handle the request, process the registration, and return to the client.

CGI programming in Scheme

PLT Scheme ships with a CGI library, cgi.ss, that will help you handle this. Lets review some of the nomenclature, or types, used by this library:

binding
an association of a form item with its value.
bindings
a list of binding's.
html-string
any text string that has been escaped according to HTML conventions.

It should be clear now that what we want to do in order to retrieve the information provided by the user in the form is to get the bindings, search for the values of the Name, and Email keys.

(let* ((b (get-bindings))
       (name (extract-binding/single "Name" b))
       (email (extract-binding/single "Email" b)))

We used get-bindings, which returns the whole set of bindings delivered by the client. Note that there is also a get-bindings/post, and a get-bindings/get, which only work when the method POST, and GET are used respectively, while get-bindings determines the kind of method used, and invokes the appropriate procedure.

extract-binding/single returns the value associated to the key passed as its first argument in the set of bindings passed as its second argument. The result is either a string representing the value, or a web page with an error, for instance when there was more than one value associated to the key, or the key wasn't found.

When there is more than one value, or an unknown number of values, associated to a single key, you should use extract-bindings, which returns a list with zero, one, or many associations for the key.

After getting, and processing the information (validate it, store it in a database, etc.), you will want to return to the client. The cgi.ss library provides one convenient way to do just that:

  (generate-html-output
   "Thank you!"
   (list (format "Dear ~a, " name)
         "We have received your information, and this completes the registration process. "
         "Thank you!"))

And that's it. There are a couple of other procedures provided by the cgi.ss library, but this should cover the basis.

Important: In order to actually be able to run this example (execute the small scheme script above), you need to have a Web server properly configured, the script has to reside in the right place and have the right permissions, and the URI depicted in the action field, "/servlets/doregister.ss", must point to it.

-- FranciscoSolsona - 06 Apr 2004

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest