10.1 the Common Gateway Interface 10.2 CGI Linkage 10.2 CGI Linkage (Continued)
Total Page:16
File Type:pdf, Size:1020Kb
10.1 The Common Gateway Interface 10.2 CGI Linkage 10.2 CGI Linkage (continued) - Markup languages cannot be used to specify - CGI programs often are stored in a directory named <!-- reply.html - calls a trivial cgi program cgi-bin computations, interactions with users, or to --> provide access to databases <html> - Some CGI programs are in machine code, but Perl <head> programs are usually kept in source form, so <title> - CGI is a common way to provide for these needs, perl must be run on them by allowing browsers to request the execution of HTML to call the CGI-Perl program reply.pl </title> server-resident software - A source file can be made to be “executable” by </head> adding a line at their beginning that specifies that <body> - CGI is just an interface between browsers and a language processing program be run on them This is our first CGI-Perl example servers first <a href = "http://www.cs.ucp.edu/cgi-bin/reply.pl"> - An HTTP request to run a CGI program specifies a For Perl programs, if the perl system is stored in Click here to run the CGI program, reply.pl </a> program, rather than a document /usr/local/bin/perl, as is often is in UNIX </body> systems, this is - Servers can recognize such requests in two ways: </html> #!/usr/local/bin/perl -w 1. By the location of the requested file (special - The connection from a CGI program back to the subdirectories for such files) - An HTML document specifies a CGI program with requesting browser is through standard output, the hypertext reference attribute, href, of an anchor usually through the server 2. A server can be configured to recognize tag, <a>, as in executable files by their file name extensions - The HTTP header needs only the content type, <a href = followed by a blank line, as is created with: - A CGI program can produce a complete HTTP "http://www.cs.uccs.edu/cgi-bin/reply.pl>" response, or just the URL of an existing document Click here to run the CGI program, reply.pl print "Content-type: text/html \n\n"; </a> Chapter 10 © 2003 by Addison Wesley Longman, Inc. 1 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 2 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 3 10.2 CGI Linkage (continued) 10.3 Query String Format 10.4 The CGI.pm Module #!/usr/local/bin/perl - A query string includes names and values of - A Perl module serves as a library # reply.pl – a CGI program that returns a widgets # greeting to the user - The Perl use declaration is used to make a module - Widget values are always coded as strings available to a program print "Content-type: text/html \n\n", "<html> <head> \n", - The form of a name/value pair in a query string is: - To make only part of a module available, specify "<title> reply.pl example </title>", name=value the part name after a colon " </head> \n", "<body> \n", "<h1> Greetings from your Web server!", - If the form has more than one widget, their values (For our purposes, only the standard part of the " </h1> \n </body> </html> \n"; are separated with ampersands CGI module is needed) milk=2&payment=visa use CGI ":standard"; - Each special character is coded as a percent sign - Common CGI.pm Functions and a two-character hexadecimal number (the ASCII code for the character) - “Shortcut” functions produce tags, using their parameters as attribute values - Some browsers code spaces a plus signs, rather than as %20 - e.g., h2("Very easy!"); produces <h2> Very easy! </h2> - In this example, the parameter to the function h2 is used as the content of the <h2> tag Chapter 10 © 2003 by Addison Wesley Longman, Inc. 4 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 5 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 6 10.4 The CGI.pm Module (continued) 10.4 The CGI.pm Module (continued) 10.4 The CGI.pm Module (continued) - If both content and attributes are passed to a - The start_html function is used to create the - Tags can have both content and attributes function, the attributes are specified in a hash head of the return document, as well as the literal as the first parameter <body> tag - Each attribute is passed as a name/value pair, a({-href => "fruit.html"}, - The parameter to start_html is used as the title just as in a hash literal "Press here for fruit descriptions"); of the document - Attribute names are passed with a preceding Output: <a href="fruit.html"> dash Press here for fruit descriptions</a> start_html("Bill’s Bags"); textarea(-name => "Description", - Tags and their attributes are distributed over DOCTYPE html PUBLIC -rows => "2", the parameters of the function "-//W3C//DTD XHTML 1.0 Transitional//EN" -cols => "35" "DTD/xhtml11-transitional.dtd"> <html xmlns= ); ol(li({-type => "square"}, ["milk", "bread", "cheese"])); "http://www.w3.org/1999/xhtml lang="en-US"> Produces: <head><title>Bill’s Bags</title> </head><body> Output: <ol> <textarea name ="Description" rows=2 <li type="square"milk</li> - The param function is given a widget’s name; it cols=35> </textarea> <li type="square"bread</li> returns the widget’s value <li type="square"cheese</li> </ol> - If the query string has name=Abraham in it, - CGI.pm also includes non-shortcut functions, which param("name") will return "Abraham" produce output for return to the user - The end_html function generates </body></html> - A call to header() produces: Content-type: text/html;charset=ISO-8859-1 SHOW popcorn.html , its display, and -- blank line -- popcorn.pl Chapter 10 © 2003 by Addison Wesley Longman, Inc. 7 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 8 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 9 10.5 A Survey Example 10.5 A Survey Example (continued) 10.5 A Survey Example (continued) - We will use a form to collect survey data from - The program to collect and record form data must: - Tables are easier to specify with CGI.pm users 1. Decode the data in the query string - The table is created with the table function - The program needs to accumulate survey results, - The border attribute is specified as a parameter which must be stored between form submissions 2. Determine which row of the file must be modified - Store the current results in a file on the server - The table’s caption is created with a call to caption, as the second parameter to table - Because of concurrent use of the file, it must be 3. Open, lock, read, unlock, and close the survey protected from corruption by blocking other data file - Each row of the table is created with a call to accesses while it is being updated Tr 4. Split the affected data string into numbers and - Under UNIX, this can be done with the Perl store them in an array - A heading row is created with a call to th function, flock, using the parameter value 2 to - Data cells are created with calls to td specify a lock operation and 8 to specify an 5. Modify the affected array element and join the unlock operation array back into a string - The calls to Tr, th, and td require references as --> SHOW conelec.html and its display parameters 6. Open, lock, write, unlock, and close the survey - Two CGI programs are used for this application, data file - Suppose we have three arrays of sales numbers, one to collect survey submissions and record the one for each of three salespersons; each array new data, and one to produce the current totals --> SHOW conelec1.pl has one value for each day of the work week - The file format is eight lines, each having seven - We want to build a table of this information, values, the first four for female responses and the using CGI.pm last four for male responses Chapter 10 © 2003 by Addison Wesley Longman, Inc. 10 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 11 Chapter 10 © 2003 by Addison Wesley Longman, Inc. 12 10.5 A Survey Example (continued) 10.5 A Survey Example (continued) 10.5 A Survey Example (continued) conelec2.pl table({-border => "border"}, - The program that produces current results must: --> SHOW caption("Sales Figures"), --> SHOW Figure 10.7 Tr( 1. Open, lock, read the lines into an array of [th(["Salesperson", "Mon", "Tues", strings, unlock, and close the data file "Wed", "Thu", "Fri"]), 10.6 Cookies th("Mary").td(\@marysales), th("Freddie").td(\@freddiesales), 2. Split the first four rows (responses from - A session is the collection of all of the requests th("Spot").td(\@spotsales), females) into arrays of votes for the four age made by a particular browser from the time the ] groups ) browser is started until the user exits the browser ); 3. Unshift row titles into the vote rows (making - The HTTP protocol is stateless them the first elements) - But, there are several reasons why it is useful for 4. Create the column titles row with th and put its the server to relate a request to a session address in an array - Shopping carts for many different simultaneous 5. Use td on each rows of votes customers - Customer profiling for advertising 6. Push the addresses of the rows of votes onto the row address array - Customized interfaces for specific clients 7. Create the table using Tr on the array of row - Approaches to storing client information: addresses - Store it on the server – too much to store! 8. Repeat Steps 2-7 for the last four rows of data (responses from males) - Store it on the client machine - this works Chapter 10 © 2003 by Addison Wesley Longman, Inc.