Figures 19.1319.14 show the implementation of a simple interactive portal for the fictitious Bug2Bug Travel Web site. The example queries the client for a name and password, then displays information about weekly travel specials based on the data entered. For simplicity, the example does not encrypt the data sent to the server. Ideally, sensitive data like a password should be encrypted. Encryption is beyond the scope of this book.
Figure 19.13. Interactive portal to create a password-protected Web page.
(This item is displayed on pages 939 - 940 in the print version)
"http://www.w3.org/1999/xhtml"> 9 10
Figure 19.14. Interactive portal handler.
(This item is displayed on pages 940 - 942 in the print version)
Figure 19.13 displays the opening page. It is a static XHTML document containing a form that POSTs data to the portal.cgi CGI script (line 16). The form contains one field to collect the user's name (line 18) and one to collect the user's password (line 19). [Note: Not like the CGI scripts, which are placed in the cgi-bin directory of the Web server, this XHTML document was placed in the htdocs directory of the Web server.]
Figure 19.14 contains the CGI script. First, let us examine how the user's name and password are retrieved from standard input and stored in strings. The string library find function searches dataString (line 30) for an occurrence of namebox=. Function find returns a location in the string where namebox= was found. To retrieve the value associated with namebox=the value entered by the userwe move the position in the string forward 8 characters. The program now contains an integer "pointing" to the starting location. Recall that a query string contains name-value pairs separated by equals signs and ampersands. To find the ending location for the data we wish to retrieve, we search for the & character (line 31). The length of the entered word is determined by the calculation endNamelocation - namelocation. We use a similar approach to determine the start and end locations of the password (lines 3233). Lines 3638 assign the form-field values to variables nameString and passwordString. We use nameString in line 52 to output a personalized greeting to the user. The current weekly specials are displayed in lines 5356.
If the member password is correct, lines 5960 output additional specials. If the password is incorrect, the client is informed that the password was invalid and no additional specials are displayed.
Note that we use a static Web page and a separate CGI script here. We could have incorporated the opening XHTML form and the processing of the data into a single CGI script, as we did in previous examples in this chapter. We ask the reader to do this in Exercise 19.8.
Performance Tip 19.1
It is always much more efficient for the server to provide static content rather than execute a CGI script, because it takes time for the server to load the script from hard disk into memory and execute the script (whereas an XHTML file needs to be sent only to the client). It is a good practice to use a mix of static XHTML (for content that generally remains unchanged) and CGI scripting (for dynamic content). This practice allows the Web server to respond to clients more efficiently than if only CGI scripting were used. |
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography