CSC 223 | Grinnell College | Fall, 2006 |
Software Design | ||
The lab on An Introduction to Dynamic Web Pages with Java provided an overview of interactions between a browser and server. After considering static Web pages, that lab introduced the basics of CGI programming to allow the construction of dynamic Web pages. The latter part of that lab showed the use of forms which allow a user to enter information into a Web page and transmit it to a Java program for later processing. The Java program then can utilize these data in preparing a Web-page response for the user.
This lab considers mechanisms to provide some continuity from one Web page to another, as part of an extended user session. In particular, this lab considers two mechanisms to enhance communication between a browser and server:
Both of these approaches are illustrated in the Web page, java-form-cookies.html that extends the example from the previous lab.
From your experience with fac-directory-java.html in the lab on dynamic pages, you know how much of this interaction works:
With this review, we look at the new pages java-form-cookies.html more closely.
Copy the following files to your cgi-bin directory:
Compile javaVisits.java, and make the html, cgi, and class files public.
Check that your version of java-form-cookies.html works in your browser and that it appropriately calls your versions of these programs. (You may need to edit the CLASSPATH in the cgi file to refer to your account.)
These observations illustrate that forms allow the embedding of hidden fields within forms on a Web page. If the Web page were generated from a CGI script, the CGI script could print hidden fields as well as visible boxes and other page elements. These hidden fields then can be used in subsequent processing. This is illustrated in the following application for elementary school arithmetic.
A dynamic Web page is to be constructed to give elementary school students practice with simple addition. The idea is to generate two integers between 0 and 9 (inclusive), and form a Web page in which a student enters the sum. After clicking "submit", the answer is checked, and the student gets a "right" or "wrong" response.
This problem will involve a cgi script and corresponding Java program:
To clarify, the student would see something like the following when first typing the URL:
As a technical note, Java contains a random number generator in java.lang.Math. In particular, Math.random() generates real numbers between 0 and 1 (including 0, but excluding 1). Thus, (int)Math.floor(10.0*Math.Random()) generates integers between 0 and 9 (inclusive).
When a browser accesses a Web page, the Web server can request that your browser store a small piece of information, called a cookie, on your machine. When you go to this Web page again, the Web server can ask for this cookie and can use that cookie in processing.
Check if your browser is usually set to accept cookies.
If your browser does not usually accept cookies, change the setting for now, and redo Step 1. You can change the settings back at the end of this lab.
The form-cookie example you have already seen, java-form-cookies.html, illustrates how a program can utilize cookies to track information when working with a browser. The basic idea is to print one or more lines Set-Cookie: at the very start of a program (after the Content-type line, but before the blank line before <!DOCTYPE). CGI script java-visits.cgi and program javaVisits.java show a typical scenario.
java-visits.cgi has a similar form to the cgi scripts of the previous lab:
#!/bin/bash export CLASSPATH=":/home/walker/public_html/cgi-bin/examples" /opt/jdk1.5.0/bin/java javaVisits "$QUERY_STRING" "$HTTP_COOKIE"
As this illustrates, java-visits.cgi passes two strings to the Java program.
javaVisits.java receives these two strings as parameters to main. The cookie information is organized as a sequence of name=value pairs, separated by semi-colons (just as a query string contains name=value pairs when the information comes from a form).
System.out.println("Set-Cookie: numVisits=" + (numVisits+1)); System.out.println("Set-Cookie: lastDate=" + todaysDate.getTime() +"; expires=" + nextWeek.getTime());
In the first case, a value is associated with the field numVisits, while in the second case, today's date is associated with the field lastDate.
In Web use, cookies have an associated expiration time. This can be set explicitly with an expires= string, as in the second line above. However, if this is not set, then the cookie will expire when your browser session is done (e.g., when you close your browser).
Once set, the values are sent to the browser, and this information is returned to the server whenever the browser links again to this server. Thus, cookies provide a convenient way for a server to set a simple history of past work, and that information can be used in future processing. Note that this information is not displayed in the URL or query string. Further, the information is not visible on the Web page (in contrast to hidden fields that are visible when you view the page source).
Review program javaVisits.java, and give a careful description of how processing proceeds in each section of the code.
Overall, javaVisits.java uses cookies numVisits and lastDate to keep track of how often a page is used in the current session and of when the user last accessed this material. If the values of these variables are null, the program assumes these have not been set previously, so initial values are assigned. Otherwise, the program retrieves past values for later processing.
The same idea can be used to keep of the number of right and wrong answers for the simple-addition.cgi script and associated SimpleAddition.java program from Step 6.
http://www.walker.cs.grinnell.edu/courses/223.fa05/labs/cookies-forms.shtml
created 6 October 2005 last revised 7 October 2005 | |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |