Fundamentals of CS I (CS151 2002F)

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.ss 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.ss

The lines must be flush against the left margin!

d. Share all three files.

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: Testing within DrScheme

a. Load greeting.ss from within DrScheme.

b. Find the place in which testing is defined and make sure that it is defined as true (#t).

c. Execute greeting.ss.

d. Type (page) and see what happens.

e. Find the code that defines user and change the string "Sam" to your name.

f. Type (page) and see what happens.

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

Exercise 2: Testing From the Web

Things may go seriously wrong in this step. 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 3: Responding to Input

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

a. Within DrScheme, change the line in greeting.ss that reads (define testing #t) to (define testing #f).

b. What happens when you try to execute the program and run (page) from within DrScheme? (Yes, you may get an error.)

c. What happens when you repeat the steps of the previous exercise?

Exercise 4: Inserting Errors

a. Reset the value of testing to #t.

b. Change the body of page to read (greeting-page person).

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

d. Verify your answer experimentally.

e. 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)?

f. Verify your answer experimentally.

g. Reset the value of testing to #f.

h. 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)?

i. Verify your answer experimentally.

j. Fix the error you introduced in step b. above.

Exercise 5: 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.ss 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 6: 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 7: 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

Friday, 9 February 2001 [Samuel A. Rebelsky]

Monday, 16 September 2002 [Samuel A. Rebelsky]

 

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 Mon Dec 2 09:18:43 2002.
The source to the document was last modified on Tue Sep 17 10:31:40 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/cgi.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu