Professional Documents
Culture Documents
Web
Programming
This is the common air that
bathes the globe.
Walt Whitman
OBJECTIVES
In this chapter you will learn:
Bernard Malamud
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Self-Review Exercises
19.1
19.2
State whether each of the following is true or false. If false, explain why.
a) Web servers and clients communicate with each other through the platform-independent HTTP.
ANS: True.
b) Web servers often cache Web pages for reloading.
ANS: True.
c) The information tier implements business logic to control the type of information that
is presented to a particular client.
ANS: False. The middle tier implements business logic to control interactions between application clients and application data.
d) A dynamic Web page is a Web page that is not created programmatically.
ANS: False. A dynamic Web page is a Web page that is created programmatically.
e) We put data into a query string using a format that consists of a series of name-value
pairs joined with exclamation points (!).
ANS: False. The pairs are joined with an ampersand (&).
f) Using a CGI script is more efficient than using an XHTML document.
ANS: False. XHTML documents are more efficient than CGI scripts, because they do not
need to be executed on the server side before they are output to the client.
g) The post method of submitting form data is preferable to get when sending personal
information to the Web server.
ANS: True.
Exercises
19.3
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
b) Multitier application.
ANS: Multitier applications divide functionality into separate tiers (i.e., logical groupings
of functionality). Tiers can be located on the same computer or on separate computers.
c) Request method.
ANS: A request method specifies how a client makes requests from a server. The two most
common request methods are get and post.
19.4 Explain the difference between the get request type and the post request type. When is it
ideal to use the post request type?
ANS: A get request gets (or retrieves) information from a server. Such requests often retrieve an HTML document or an image. A post request posts (or sends) data to a
server, such as authentication information or data from a form that gathers user input. Usually, post requests are used to post a message to a news group or a discussion
forum, pass user input to a data-handling process and store or update the data on a
server. A get request sends form data as part of the URL. A get request limits the
query string to a predefined number of characters that varies from server to server. If
the query string exceeds this limit, a post request must be used. The data sent in a
post request is not part of the URL and cannot be seen by users. Sensitive form fields,
such as passwords, should be sent using post requests.
19.5
Write a CGI script that prints the squares of the integers from 1 to 10 on separate lines.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
19.6 Write a CGI script that receives as input three numbers from the client and displays a statement indicating whether the three numbers could represent an equilateral triangle (all three sides
are the same length), an isosceles triangle (two sides are the same length) or a right triangle (the
square of one side is equal to the sum of the squares of the other two sides).
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
if ( getenv( "CONTENT_LENGTH" ) )
{
contentLength = atoi( getenv( "CONTENT_LENGTH" ) );
cin.read( postString, contentLength );
dataString = postString;
} // end if
cout << "Content-type: text/html\n\n"; // output HTTP header
// output XML declaration and DOCTYPE
cout << "<?xml version = \"1.0\"?>"
<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
<< "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
// output html element and some of its contents
cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">"
<< "<head><title>Using POST with Forms</title></head><body>";
// output xhtml form
cout << "<p>Enter three integers:</p>"
<< "<form method = \"post\" action = \"ex19_06.cgi\">"
<< "One<br /><input type = \"text\" name = \"one\" /><br />"
<< "Two<br /><input type = \"text\" name = \"two\" /><br />"
<< "Three<br /><input type = \"text\" name = \"three\" /><br />"
<< "<br /><input type = \"submit\" value = \"Submit Numbers\" />"
<< "</form>";
// data was sent using POST
if ( contentLength > 0 )
{
// retrieve first value
int oneLocation = dataString.find( "one=" ) + 4;
int endOne = dataString.find( "&two" ) ;
oneString = dataString.substr( oneLocation, endOne - oneLocation );
// retrieve second value
int twoLocation = dataString.find( "two=" ) + 4;
int endTwo = dataString.find( "&three" ) ;
twoString = dataString.substr( twoLocation, endTwo - twoLocation );
// retrieve third value
int threeLocation = dataString.find( "three=" ) + 6;
threeString = dataString.substr( threeLocation );
// convert values to int
int one = atoi( oneString.c_str() );
int two = atoi( twoString.c_str() );
int three = atoi( threeString.c_str() );
// equilateral triangle
if( one == two && two == three )
cout << "This is an equilateral triangle";
// isosceles triangle
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
6
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
if( ( one == two && one != three ) || ( two == three && two != one )
|| ( one == three && one != two ) )
cout << "This is an isosceles triangle";
// square sides of triangle
int onesquared = one * one;
int twosquared = two * two;
int threesquared = three * three;
// right triangle
if ( ( onesquared == ( twosquared + threesquared ) ) ||
( twosquared == ( onesquared + threesquared ) ) ||
( threesquared == ( onesquared + twosquared ) ) )
cout << "This is a right triangle.";
} // end if
else // no data was sent
cout << "<p>Please enter data in the form.</p>";
cout << "</body></html>";
return 0;
} // end main
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
19.7 Write a soothsayer script that allows the user to submit a question. When the question is
submitted, the script should choose a random response from a list of answers (such as "It could
be", "Probably not", "Definitely", "Not looking too good" and "Yes") and display the answer
to the client.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<<
<<
<<
<<
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
19.8 Modify the program of Figs. 19.1319.14 to incorporate the opening XHTML form and
the processing of the data into a single CGI script (i.e., combine the XHTML of Fig. 19.13 into the
CGI script of Fig. 19.14). When the CGI script is requested initially, the form should be displayed.
When the form is submitted, the CGI script should display the result.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
73
74
75
76
77
78
79
80
81
82
83
84
11
19.9 Modify the viewcart.cgi script (Fig. 19.23) to enable users to remove some items from the
shopping cart.
ANS:
1
2
3
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
12
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using
using
using
using
std::cerr;
std::cin;
std::cout;
std::ios;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <cstdlib>
using std::getenv;
using std::atoi;
using std::exit;
void displayShoppingCart( const string & );
int main()
{
char query[ 1024 ] = ""; // variable to store query string
string cartData; // variable to hold contents of cart
string dataString = "";
string cookieString = "";
string isbnEntered = "";
string addString = "";
int contentLength = 0;
// retrieve cookie data
if ( getenv( "HTTP_COOKIE" ) )
{
cartData = getenv( "HTTP_COOKIE" );
cookieString = cartData;
} // end if
// data was entered
if ( getenv( "CONTENT_LENGTH" ) )
{
contentLength = atoi( getenv( "CONTENT_LENGTH" ) );
cin.read( query, contentLength );
dataString = query;
// find location of isbn value
int addLocation = dataString.find( "add=" ) + 4;
int endAdd = dataString.find( "&isbn" );
int isbnLocation = dataString.find( "isbn=" ) + 5;
// retrieve isbn number to add to cart
addString = dataString.substr( addLocation, endAdd - addLocation );
// retrieve isbn number to add to cart
isbnEntered = dataString.substr( isbnLocation );
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// write cookie
string expires = "Friday, 14-MAY-10 16:00:00 GMT";
int cartLocation = cookieString.find( "CART=" ) + 5;
if ( cartLocation > 4 ) // cookie exists
cookieString = cookieString.substr( cartLocation );
if( addString == "true" ) // add book to shopping cart
{
if ( cookieString == "" ) // no cookie data exists
cookieString = isbnEntered;
else // cookie data exists
cookieString = cookieString + "," + isbnEntered;
// set cookie
cout << "set-cookie: CART=" << cookieString << "; expires="
<< expires << "; path=\n";
} // end if
if ( addString == "false" ) // remove book from shopping cart
{
string::size_type x = cookieString.find( isbnEntered );
// remove all instances of ISBN
while ( x < string::npos )
{
cookieString.replace( x, 13, "" );
x = cookieString.find( isbnEntered );
} // end while
// set cookie
cout << "set-cookie: CART=" << cookieString << "; expires="
<< expires << "; path=\n";
} // end if
} // end if
cout << "Content-Type: text/html\n\n"; // output HTTP header
// output XML declaration and DOCTYPE
cout << "<?xml version = \"1.0\"?>"
<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
<< "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
// output html element and some of its contents
cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">"
<< "<head><title>Shopping Cart</title></head><body><center>";
if ( cookieString != "" ) // cookie data exists
{
cout << "<p>Here is your current order:</p>";
displayShoppingCart( cookieString );
} // end if
else
cout << "<p>The shopping cart is empty.</p>";
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
13
14
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
bookString = "";
yearString = "";
isbnString = "";
priceString = "";
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
15
166
int count = 0;
167
168
// match has been made
169
while ( match >= 0 && isbnString != "" )
170
{
171
count++;
172
match = cookieRef.find( isbnString, match + 13 );
173
} // end while
174
175
// output table row with book information
176
if ( count != 0 )
177
cout << "<tr>"
178
<< "<form method=\"post\" action=\"/cgi-bin/viewcart.cgi\">"
179
<< "<input type=\"hidden\" name=\"add\" value=\"false\"/>"
180
<< "<input type=\"hidden\" name=\"isbn\" value=\""
181
<< isbnString << "\"/><td>" << bookString << "</td><td>"
182
<< yearString << "</td><td>" << isbnString << "</td><td>"
183
<< priceString << "</td><td>" << count << "</td>"
184
<< "<td><input type=\"submit\" value=\"Remove\"/>\n</td>"
185
<< "</form></tr>";
186
} // end while
187
188
cout << "</table>"; // end table
189 } // end function displayShoppingCart
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
16
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.