Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Summary: As you may know, it is possible to write programs that generate Web pages in response to user input (input that is often entered on other Web pages). The most common mechanism for writing such programs is CGI, which stands for common gateway interface. It is possible to write CGI programs (usually called "scripts") in almost any programming language, including Scheme.
Contents:
Here's how CGI typically works.
So, what do you need to learn in order to write CGI scripts? You already know how to write Scheme to generate an HTML page (or you should if you did the strings lab). So, you have to learn how to get information from the server, tell the server to run your program from a CGI script, and build the HTML page that lets someone enter information.
I've set up some helper programs that make it easier (but not necessarily easy) for you to write CGI scripts in Scheme. I also recommend that you follow a standard process when building your scripts.
Here are the steps that I recommend that you undertake when writing a CGI script.
Let's start with what you should already mostly know how to do.
user
.
.ss
file. For example,
greetings.ss
. The file should be in your
public_html
directory.
% share filename
% chmod a+r filename
Now you need to extend the file so that it works with the CGI system
(in terms of getting input from the CGI system and working when called
from the CGI system). The way I've set up my CGI gateway, it always
calls a procedure named page
that has no parameters.
(load "/home/rebelsky/Web/Scheme/webutils.ss")
page
and takes no parameters. This procedure should call your first procedure
using values extracted by
(get-cgi-variable 'name "default-value")
. For example
(define page (lambda () (greeting-page (get-cgi-variable 'user "SamR"))))
page
!
(page)
.
Now it's time to build something that ties your Scheme program to the CGI system.
#!/bin/bash /home/rebelsky/bin/schemeweb your-scheme-file
#!/bin/bash /home/rebelsky/bin/schemeweb greeting.ss
/home/rebelsky/bin
no matter
who you are!.cgi
. For example, greetings.cgi
.% chmod a+x filename.cgi % chmod a+r filename.cgi
http://www.cs.grinnell.edu/~username/file.cgi?name=value
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Examples/greetings.cgi?user=Fred
Now you're ready to build the HTML page that people will use.
Here is a Scheme procedure that generates a greeting page based on an input name. It assumes that you've written some helper procedures similar to those from the strings lab.
;;; Procedure ;;; greeting-page ;;; Parameters: ;;; user-name, a string that represents the name of a user ;;; Purpose: ;;; Generates an HTML page that can greet the named person. ;;; Produces: ;;; page-code, a string that corresponds to that page. ;;; Preconditions: ;;; user must be nonempty. ;;; Postconditions: ;;; page-code is valid HTML. (define greeting-page (lambda (user-name) (make-page (head "Greetings") (body (string-append (heading 1 "Welcome") (string #\newline) (paragraph (string-append "Hi " user-name "!")))))))
Here is the extra UI code one might use to build the page:
(define page (lambda () (greting-page (get-cgi-variable 'user "SamR"))))
As mentioned above, a form is simply some HTML that lets the user enter some input and ties that input to a CGI script. (Forms can do other things, too, but this purpose is enough for now).
As you might expect, you surround a form with form
tags. The
opening tag must also include two parameters:
method
, which can be put
or get
.
I recommend that you use get
while testing. However, if
you expect the user to enter a lot of input, use put
.
action
, which should be the URL of your CGI script. It can
just be the name of your script if the HTML file is in the same directory.
For example,
<form method="get" action="greeting.cgi"> ... </form>
You can include normal HTML in a form. Normal HTML is displayed normally and is not uploaded to the server when the user submits values. Rather, it is used to give some information to the user about where things might go.
When you want the user to type a value, you use an input
tag
with the following parameters:
type
, which must be text
name
, which is the name you want assigned to the value
in the field. For our example, this will be user
.
value
, which is the default value.
For example,
<input type="text" name="user" value="">
You should also provide a handy-dandy "Click Here" button. Once again,
you use an input
tag. This time, you use slightly different
parameters:
type
, which must be submit
value
, which provides the text in the button
For example
<input type="submit" value="Enter your name and click here!">
That's it, you know the basics. If you want things other than fields in your forms, ask me about them in class or look them up on the Web.
Here's how CGI works in my recommended environment.
/home/rebelsky/bin/schemeweb
).
(page)
procedure it finds in the
file you specified.(page)
procedure grabs the information and
generates a string which it returns to my script.
(stored separately; select the link)
greeting.ss
greeting.cgi
:
#!/bin/bash /home/rebelsky/bin/schemeweb greeting.ss
<html> <head> <title>Testing Scheme CGI</title> </head> <body> <form method="get" action="greeting.cgi"> <input type="text" name="user" value=""> <br> <input type="submit" value="Enter Your Name and Click Me!"> </form> </body> </html>
Wednesday, 7 February 2001 [Samuel A. Rebelsky]
Friday, 16 February 2001 [Samuel A. Rebelsky]
chmod 755
to something slightly clearer.
Monday, 16 September 2002 [Samuel A. Rebelsky]
Friday, 7 February 2003 [Samuel A. Rebelsky]
webutils.ss
library, which
is much easier than DrScheme's built-in library.
Monday, 10 February 2003 [Samuel A. Rebelsky]
share
.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 6 09:21:20 2003.
The source to the document was last modified on Mon Feb 10 08:18:34 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Readings/cgi.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby