Computer Science Fundamentals (CS153 2003S)

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

Note that you may have to switch to your public_html directory before executing those commands. You do so with

% cd public_html

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

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 2: 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 3: 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 4: Selecting Pages

a. Add the following procedure to greeting.ss.

(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 5: 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 6: 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 7: 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 8: 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.

Exercise 9: Working with Numbers

a. Write a CGI script that takes a number as input and produces the square root of that number as output. Since CGI variables are passed as strings, you will probably want to use the string->number and number->string procedurese to convert to and from numbers.

b. Write a CGI script that takes a, b, and c for ax2 + bx + c as input and prints out the roots of that quadratic.

Exercise 10: Your Own Page

a. Using the mark procedure you created for the lab on strings, write a Scheme program that builds a page.

b. Test your program from within DrScheme.

c. Add appropriate infrastructure to run it from the Web.

 

History

Friday, 9 February 2001 [Samuel A. Rebelsky]

Monday, 16 September 2002 [Samuel A. Rebelsky]

Monday, 10 February 2003 [Samuel A. Rebelsky]

Tuesday, 11 February 2003 [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 Tue May 6 09:19:43 2003.
The source to the document was last modified on Tue Feb 11 09:25:18 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/cgi.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu