CPSC 225: Intermediate Programming, Spring 2003
Programming Assignment 2: CGI, Part 1



SOME WEB PAGES ARE "active". An active web page might be interactive, like a Web survey that lets the user enter data into a form. Or it might display data that varies with time, like a weather forecast, so that the same page looks different when you visit it at different times. Both types of pages are based on "CGI programs." A CGI program is a program that creates a Web page on demand. When you use a web browser to access a CGI program, the page that you get does not come from a simple, static file on the server. Instead, the page is generated by running a CGI program. The output of the CGI program is sent to your Web browser and becomes the html source for the page that you see. When you fill in a form on a Web page, the data that you submit is processed by a CGI program. That CGI program also creates the Web page that you get back as a response (often just thanking you for submitting the data). Pages that change with time like weather forecasts are generated directly by CGI programs.

The math server is configured so that users can use CGI programs on their sites. To use a C++ program for CGI, the compiled program must be in your www directory, and the name of the compiled program must end with ".cgi". When the server gets a request for a file that ends with .cgi, it assumes that the file is a CGI program and it executes that program. Everything that the program writes to cout is sent back to the Web browser. That is, the CGI program generates the Web page by writing the html source code of the page to cout (plus two extra lines at the top -- see the sample program). The program can do anything it wants to generate the page. It could, for example, read some data from a file, do some calculations with the data, and display the results of the calculation on the Web page. That's what you will be doing in this assignment.

This assignment and the next will both be CGI programs. For the two assignments, you should design a Web survey. It can be on any topic that you like. The data submitted by people who take the survey will be stored in a file. Each time someone submits data for the survey, that data should be added onto the end of a file in your user account. There will only be one file, but it will contain the answers from all the people who take the survey. You should design the survey on paper now, and decide on a format for the data file. You should probably limit the questions to yes/no questions, multiple-choice questions, and questions that have a numerical answer. This will let you use a simple file format such as representing all the answers as numbers (1 for yes, 0 for no, etc.), and putting each user's response on one line of the file.

Actually putting the survey on the Web and processing responses will be the topic of the next assignment. Usually when you fill out a survey, you want to see some statistics about all the data that has been collected from you and from other people. Web surveys often provide a page where you can see statistics about the data that has been collected. This page can be produced by a CGI program that reads all the data, computes the statistics, and generates a page to display them. For this assignment, you should write a CGI program that performs this task for your program.

To test your program for now, make a sample file of fake data that could have come from your survey. When the program runs, it should read all the data from the file and compute some statistics. For example, it could count the number of people who gave each possible response to a multiple choice question. Or it could find the average answer for a question that has a numerical response. The program should create a reasonably attractive and informative Web page that displays the statistics.

You should put the program on your Web site. If the data file is in the same directory with your compiled CGI program, you can use a simple name such as "survey.dat" in your program to refer to the data file. It is also possible to put the data file in another directory and refer to it using a full path name such as "/home/jsmith/cs225/survey.dat".

The sample file showresults.cc is a very simple CGI program. It creates a Web page that shows a table of data, but the data in the table is not computed from a file. If you need a very quick refresher on the basics of HTML see Section 6.2 of my on-line Java textbook. If you want to know more about HTML tables, you might look at this CS371 lab, and at my TableDemo.

You should turn in a printout of your program. On the printout write the URL of the program on your Web site. The program is due in class next Wednesday, Janyary 29.


David Eck