Fundamentals of Computer Science I (CS151 2003F)

Laboratory: CGI Scripting

Summary: This lab reviews some mechanics related to the use of Scheme for CGI Scripts in CSC151.

Useful Files:

Contents:

Exercises

Exercise 0: Preparation

a. Make sure you've done the reading on CGI.

b. Make copies of greeting.scm and greeting.html in your public_html directory.

c. Create (using gedit or DrScheme) the file greeting.cgi in your public_html directory that contains the following two lines

#!/bin/bash
/home/rebelsky/bin/schemeweb greeting.scm

The lines must be flush against the left margin!

d. Share all three files. You should use a command like the following for each file (type it in a terminal window).

% chmod a+r filename

Don't type the percent sign.

e. Make greeting.cgi executable by typing the following in your terminal window:

% chmod a+x greeting.cgi

Don't type the percent sign.

Exercise 1: A Web Library

Look at webutils.scm (by following that link).

a. See if you can tell what the following procedures do.

Note that you can test each procedure by loading the file in DrScheme with

(load "/home/rebelsky/Web/Scheme/webutils.scm")

and then trying sample expressions involving those procedures.

b. See if you can tell how markup accomplishes its task.

Exercise 2: Generating HTML from Scheme

a. Open greeting.scm from within DrScheme.

b. Scan through the code and see if you can understand what each procedure does. Focus on greeting-page and page.

Exercise 3: Testing within DrScheme

b. Execute greeting.scm.

c. Type (page) and see what happens.

d. Find the code that defines user and change the string "SamR" to your name.

e. Type (page) and see what happens.

f. Type (greeting-page "Fred") and see what happens.

Exercise 4: Testing From the Web

Things may go seriously wrong in this step and subsequent steps. Feel free to ask lots of questions.

a. In a Web browser, open <http://www.cs.grinnell.edu/~yourid/greeting.html>.

b. Enter XXX and click the button.

c. What happens? Can you tell why?

Exercise 5: Changing Pages

a. Update the body of page to read (nice-page (get-cgi-variable 'name "Sam")).

b. What do you expect to happen when you execute your modified code and then evaluate (page) within DrScheme?

c. Verify your answer experimentally.

d. What do you expect to happen when you try the Web interface to your code (that is, when you enter values on the HTML page and then click the button)?

e. Verify your answer experimentally.

Exercise 6: Selecting Pages

a. Add the following procedure to greeting.scm.

(define custom-page
  (lambda (person)
    (if (equal? person "Sam")
        (hate-page person)
        (nice-page person))))

b. Update the body of page to use custom-page rather than nice-page (or greeting-page).

c. What effect do you expect the change to have?

d. What do you expect to happen when you try the Web interface to your code (that is, when you enter values on the HTML page and then click the button)?

e. Verify your answer experimentally.

f. What do you expect to happen when you enter SamR on the Web interface? How about Sam?

Exercise 7: Inserting Errors

a. Change the body of page to read (greeting-page (get-cgi-variable 'person "Jane")).

b. What do you expect to happen when you execute your modified code and then evaluate (page) within DrScheme?

c. Verify your answer experimentally.

d. What do you expect to happen when you try the Web interface to your code (that is, when you enter values on the HTML page and then click the button)?

e. Verify your answer experimentally.

f. Fix the error you introduced in step a above.

Exercise 8: Getting More Input

a. Add a text field to the Web page that prompts for a color.

b. See what happens when someone fills in the color (presumably nothing).

c. Update greeting.scm to store the choice in a variable. (Hint: Look at what I did for user.)

d. Update the greeting-page procedure to take the selected color as a second parameter. You must include some comment about the color in the output from greeting-page.

e. Verify within DrScheme that the new greeting-page still works.

f. Update page so that it passes both user and the color on to greeting-page.

g. Test from within your Web browser.

Exercise 9: Radio Buttons

As you may have observed in your experiences on the Web, there are many different kinds of input device on forms. One popular input device is the so-called radio button in which someone selects between a few options by clicking to the left of an option.

You can create those buttons with

<input type="radio" name="XXX" value="Val1">Text
<input type="radio" name="XXX" value="Val2">Text
<input type="radio" name="XXX" value="Val3">Text

Each radio button in a group must have the same name.

For example, here are a collection of buttons that let someone indicate what their status is at Grinnell. Note that the value specified does not need to match the text.

<input type="radio" name="status" value="5">President
<input type="radio" name="status" value="4">Administrator
<input type="radio" name="status" value="3">Student
<input type="radio" name="status" value="2">Staff
<input type="radio" name="status" value="1">Faculty

Here's how those buttons appear in a browser:

President Administrator Student Staff Faculty

a. Replace the color field in greeting.html with some radio buttons.

b. Verify that the CGI script still works with those buttons.

Exercise 10: Hidden Values

You can also encode hidden values in your forms. These are values that you want to pass on to the CGI script but that you don't want the user to change.

a. Replace the line that reads

<input type="text" name="user" value="">

with one that reads

<input type="hidden" name="user" value="John Doe">

b. What effect do you expect this change to have?

c. Verify your results experimentally.

 

History

 

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 Dec 9 13:58:58 2003.
The source to the document was last modified on Tue Nov 25 14:42:55 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/cgi.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu