|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
The basic structure of a CGI application is straightforward: initialization, processing, output, and termination. Because this section deals with concepts, flow, and programming discipline, it will use pseudocode rather than a specific language for the examples. Ideally, a script follows these steps in this order (with appropriate subroutines for do-initialize, do-process, and do-output):
InitializationThe first thing your script must do when it starts is determine its input, environment, and state. Basic operating-system environment information can be obtained the usual way: from the system registry in Windows NT, from standard environment variables in UNIX, from .ini files in Windows, and so forth. State information will come from the input rather than the operating environment or static variables. Remember, each time CGI scripts are invoked, its as if theyve never been invoked before. The scripts dont stay running between calls. Everything must be initialized from scratch, as follows:
Listing 28.8 shows the initialization phase in pseudocode: Listing 28.8 Initializing Your CGI Script, Shown in Pseudocode retrieve any operating system environment values desired allocate temporary storage for variables if environment variable REQUESTMETHOD equals GET then retrieve contents of environment variable QUERYSTRING; if QUERYSTRING is not null, parse it and decode it; else if REQUESTMETHOD equals POST then retrieve contents of environment variable QUERYSTRING; if QUERYSTRING is not null, parse it and decode it; retrieve value of environment variable CONTENTLENGTH; if CONTENT_LENGTH is greater than zero, read CONTENTLENGTH bytes from STDIN; parse STDIN data into separate variables; retrieve contents of environment variable CONTENTLENGTH; if CONTENTLENGTH equals application/x-www-form-urlencoded then decode parsed variables; else if REQUESTMETHOD is neither GET nor POST then report an error; deallocate temporary storage; terminate end if ProcessingAfter initializing its environment by reading and parsing its input, the script is ready to get to work. What happens in this section is much less rigidly defined than during initialization. During initialization, the parameters are known (or can be discovered), and the tasks are more or less the same for every script youll write. The processing phase, however, is the heart of your script, and what you do here will depend almost entirely on the scripts objectives.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |