Download presentation
Presentation is loading. Please wait.
1
COS 381 Day 23
2
Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard Also @ http://perleybrook.umfk.maine.edu/SourceCode/http://perleybrook.umfk.maine.edu/SourceCode/ In Class Work http://perleybrook.umfk.maine.edu/SourceCode/inclassWork/ Assignment 5 Due Assignment 6 is posted Due Friday, may 2 Quiz 3 covering Perl will be on Friday May 2 3 rd and final Capstone progress report due April 25 Final Capstone presentation? May 5 or May 9 @ 10AM Course evaluations Continue Discussion on Perl CGI Perl Documentation http://perldoc.perl.org/ http://perldoc.perl.org/ Perl Tutorial http://www.sthomas.net/roberts-perl-tutorial.htm http://www.sthomas.net/roberts-perl-tutorial.htm
3
New grading Rubric Old Exams (4 @ 7.5 % each) 30% Assignments (9 @ 5% each) 45% Capstone Project 15% Pre-professional Conduct (see Contract on Classroom Behavior) 10% New Exams (3 @ 10 % each) 30% Assignments (6 @ 7.5% each) 45% Capstone Project 15% Pre-professional Conduct (see Contract on Classroom Behavior) 10%
4
ETS testing ETS Field Test in Business for Wednesday, April 23, in Nadeau 109. the morning; 9:00 a.m. afternoon 1:00 PM Juniors and Seniors in the accredited Professional Management Programs (ecommerce) should take this test. If a student took the test last year, they can take it again, if they want to.
5
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming
6
9-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.1 The Common Gateway Interface Computation is required to support sophisticated web applications Computation can be done by the server or the client (browser) or both The Common Gateway Interface (CGI) is a protocol describing a standard way of providing server-side active web content Under circumstances determined by the server, an HTTP request will cause a program to run The output from the program will be the response returned to the client making the request Data from forms will be encoded in a request sent do the server This data can be used by a CGI program
7
9-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.1 Other Approaches ASP.NET from Microsoft Executable code embedded in web pages Java Servlets and Java Server Pages Servlets are executable code in Java Java server pages (JSP) are executable code embedded in web pages
8
9-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Figure 9.1 Communications and computation using CGI
9
9-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.2 CGI Linkage There are several common ways a web server can use to determine if a web request should cause a CGI program to execute Usually, the determination is based on the target of the request Certain directories can be designated as containing CGI programs Often cgi-bin is used Certain file extensions can be designated as signifying a CGI program.pl usually identifies a Perl script
10
9-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.2 CGI Linkage A request for a CGI program to executed can be made with a simple link ( tag in HTML) This method is limited Any data sent must be hard-coded into the link The usual way for invoking CGI programs is through the action attribute of a form Data from the form is then encoded and sent with the request
11
9-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.2 CGI Program Action The CGI program generally creates the response by sending output to the standard output stream Using print in Perl The CGI program will usually have to provide the content-type header Content-type: text/html This will be the last line of the response headers and must be followed by a blank line The rest of the response is created by printing the HTML code desired to standard output
12
9-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley HTML to call the CGI-Perl program reply.pl This is our first CGI-Perl example <a href = "http://www.cs.ucp.edu/cgi-bin/reply.pl"> Click here to run the CGI program, reply.pl - The connection from a CGI program back to the requesting browser is through standard output, usually through the server - The HTTP header needs only the content type, followed by a blank line, as is created with: print "Content-type: text/html \n\n"; CGI Linkage (continued)
13
9-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley #!/usr/local/bin/perl # reply.pl – a CGI program that returns a # greeting to the user print "Content-type: text/html \n\n", " \n", " reply.pl example ", " \n", " \n", " Greetings from your Web server!", " \n \n"; http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/reply.html http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/reply.pl.txt http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/helloworld.pl http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/helloworld.pl.txt CGI Linkage (continued)
14
9-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class GGI setup I have placed a CGI directory in each of your web accounts and enabled this directory to run CGI perl scripts and do file creation Referring to this directory For perl scripts <a href=http://perleybrook.umfk.maine.edu/ classes/cos381/instructor/cgi/*.pl> For file manipulation within Perl scripts open(INDAT, "<cos381/instructor/cgi/indat.dat“) I have placed in each of your CGI directories the sample CGI programs used in this lecture Some scripts have to modified for your directory Change “instructor” to your last name open(INDAT, "<cos381/instructor/cgi/indat.dat“) open(INDAT, "<cos381/david/cgi/indat.dat“)
15
9-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.3 Query String Format Both GET and POST requests under HTTP can be used to carry form data from the browser to the server The data is formatted into a query string Each form of request includes the information in a different way In a GET request, the query string is appended to the URL of the request, with a question mark used to separate it from the first part of the URL In a POST request, the query string is sent as the data part of the request In both cases, the query string is formatted the same
16
9-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.3 Query String Format Each unit of data sent is encoded as name = value The name is usually the value of a name attribute of a form widget The value is the string representation of the widget value Several units are combined by separating them with ampersands, & Special characters in name and value are encoded The code is a percent sign, %, followed by the hexadecimal code for the character A space is encoded as %20 Some browsers will encode spaces as + payment=visa&saying=pay%20up%20now%21 or payment=visa&saying=pay+up+now%21
17
9-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.3 Query String Access When the POST method is used, the query string can be read from standard input The CONTENT_LENGTH environment variable tells how many characters can be read When The GET method is used, the query string is given by the value of the environment variable QUERY_STRING See code on page 356 $method = $ENV{'REQUEST_METHOD'}; if ($method eq "GET" ) {$request = $ENV{'QUERY_STRING'}; } elsif if ($method eq "POST" ) {$size = $ENV{'CONTENT_LENGTH'}; read(STDIN,$query,$size);} else { print “Something bad happened!”;}
18
9-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 The CGI.pm Module Writing a CGI program from scratch is very tedious Creating HTML requires numerous print statements Retrieving data from the query strings is tricky One of the reasons for Perl’s popularity for CGI programming is the powerful pattern matching facilities which greatly ease the task of parsing a query string The Perl module CGI.pm provides numerous functions to help with both of these problems
19
9-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 Shortcut Functions in CGI.pm Shortcut functions return string values containing HTML code Note, the string must be printed out to actually become part of the response Some functions take no arguments print br; puts the tag into the response Some functions can be given a single argument which becomes the content of the tag print h1(“A Header”) puts A Header into the response
20
9-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 Tag Attributes in Shortcut Functions Attributes for tags are provided as attribute/value pairs in the argument list of the shortcut function The arguments are provided in the form of a literal hash Attribute names are preceded by a hyphen, - print textarea(-name => "Description", -rows => "2", -cols => "35"); produces this in the response
21
9-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 Attributes and Content Attributes and content can both be provided to a shortcut by giving the attributes explicitly as a hash reference print a({-href => "fruit.html"}, Press here for fruit descriptions"); produces this in the response Press here for fruit descriptions If an array reference is provided for the content, a tag is created for each item, giving the tag all the specified attributes
22
9-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 More Shortcuts The head shortcut function provides a standard header The start_html function provides the beginning part of an HTML document, through the start tag The function takes one argument, the document title http://perldoc.perl.org/CGI.html
23
9-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - Tags and their attributes are distributed over the parameters of the function ol(li({-type => "square"},["milk", "bread", "cheese"])); Output: - CGI.pm also includes non-shortcut functions, which produce output for return to the user - A call to header() produces: Content-type: text/html;charset=ISO-8859-1 -- blank line -- The CGI.pm Module
24
9-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - The start_html function is used to create the head of the return document, as well as the tag - The parameter to start_html is used as the title of the document start_html("Bill’s Bags"); DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " DTD/xhtml11-transitional.dtd " > Bill’s Bags - The param function is given a widget’s name; it returns the widget’s value - If the query string has name=Abraham in it, param("name") will return "Abraham" - The end_html function generates SHOW popcorn.html, its display, and popcorn.pl popcorn.htmlpopcorn.pl The CGI.pm Module)
25
9-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 A Complete Form Example The example includes an HTML page, popcorn.html, for placing an order Also it includes a Perl CGI program for processing the data, popcorn.cgi (note: use.pl not.cgi) Note, to run this example, you must have a web server configured to run CGI programs, you cannot simply browse to the file on your local system http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/popcorn.html http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/popcorn.pl.txt
26
9-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - We will use a form to collect survey data from users - The program needs to accumulate survey results which must be stored between form submissions - Store the current results in a file on the server - Because of concurrent use of the file, it must be protected from corruption by blocking other accesses while it is being updated - This can be done with the Perl function, flock, using the parameter value 2 to specify a lock operation and 8 to specify an unlock operation --> SHOW conelec.html and its display conelec.html - Two CGI programs are used for this application, one to collect survey submissions and record the new data, and one to produce the current totals - The file format is eight lines, each having seven values, the first four lines for female responses and the last four lines for male responses 9.5 A Survey Example
27
9-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley File Format
28
9-28 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - The program to collect and record form data must: 1. Decode the data in the query string 2. Determine which row of the file must be modified 3. Open, lock, read, unlock, and close the survey data file 4. Split the affected data string into numbers and store them in an array 5. Modify the affected array element and join the array back into a string 6. Open, lock, write, unlock, and close the survey data file --> SHOW conelec1.pl conelec1.pl 9.5 A Survey Example (continued)
29
9-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - Tables are easier to specify with CGI.pm - The table is created with the table function - The border attribute is specified as a parameter - The table’s caption is created with a call to caption, as the second parameter to table - Each row of the table is created with a call to Tr - A heading row is created with a call to th - Data cells are created with calls to td - The calls to Tr, th, and td require references as parameters - Suppose we have three arrays of sales numbers, one for each of three salespersons; each array has one value for each day of the work week - We want to build a table of this information, using CGI.pm 9.5 A Survey Example (continued)
30
9-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley table({-border => "border"}, caption("Sales Figures"), Tr( [th(["Salesperson", "Mon", "Tues", "Wed", "Thu", "Fri"]), th("Mary").td(\@marysales), th("Freddie").td(\@freddiesales), th("Spot").td(\@spotsales), ] ) ); 9.5 A Survey Example (continued)
31
9-31 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - The program that produces current results must: 1. Open, lock, read the lines into an array of strings, unlock, and close the data file 2. Split the first four rows (responses from females) into arrays of votes for the four age groups 3. Unshift row titles into the vote rows (making them the first elements) 4. Create the column titles row with th and put its address in an array 5. Use td on each rows of votes 6. Push the addresses of the rows of votes onto the row address array 7. Create the table using Tr on the array of row addresses 8. Repeat Steps 2-7 for the last four rows of data (responses from males) --> SHOW conelec2.plconelec2.pl 9.5 A Survey Example (continued)
32
9-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.5 A Survey Example The survey example is keeps track of data from a simple survey There are three components conelec.html presents the form and links conelec1.cgi processes a survey conelec2.cgi presents a summary of the results http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/conelec.html http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/conelec1.pl.txt http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/conelec2.pl.txt
33
9-33 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.6 Cookies HTTP is a stateless protocol, that is, the server treats each request as completely separate from any other This, however, makes some applications difficult A shopping cart is an object that must be maintained across numerous requests and responses The mechanism of cookies can be used to help maintain state by storing some information on the browser system A cookie is a key/value pair that is keyed to the domain of the server This key/value pair is sent along with any request made by the browser of the same server A cookie has a lifetime which specifies a time at which the cookie is deleted from the browser
34
9-34 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.6 Cookies and Security Cookies are only returned to the server that created them Cookies can be used to determine usage patterns that might not otherwise be ascertained by a server Browsers generally allow users to limit how cookies are used Browsers usually allow users to remove all cookies currently stored by the browser Systems that depend on cookies will fail if the browser refuses to store them
35
9-35 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.6 Cookie Functions The cookie function takes a hash with three keys for the name, value and expiration time of a cookie The cookie value produced by this function must be passed to the header function using the –cookie key header(-cookie => $a_cookie) Calling the cookie function with no arguments produces a hash of all cookies from the current request The day_cookie.pl example illustrates using a cookie to store the last time the page was visited day_cookie.pl
36
9-36 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - A cookie is an object sent by the server to the client - Cookies are created by some software system on the server (maybe a CGI program) - Every HTTP communication between the browser and the server includes information in its header about the message - At the time a cookie is created, it is given a lifetime - Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included - A browser can be set to reject all cookies - CGI.pm includes support for cookies cookie(-name => a_cookie_name, -value => a_value, -expires => a_time_value); - The name can be any string - The value can be any scalar value - The time is a number followed by a unit code ( d, s, m, h, M, y ) 9.6 Cookies (continued)
37
9-37 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - Cookies must be placed in the HTTP header at the time the header is created header(-cookie => $my_cookie); - To fetch the cookies from an HTTP request, call cookie with no parameters - A hash of all current cookies is returned - To fetch the value of one particular cookie, send the cookie’s name to the cookie function $age = cookie( ′ age ′ ); - Example: A cookie that tells the client the time of his or her last visit to this site - Use the Perl function, localtime, to get the parts of time ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime; SHOW day_cookie.pl day_cookie.pl SHOW cookie_raider.plcookie_raider.pl http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/cookie_raider.pl.TXT http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/cookie_raider.pl.TXT 9.6 Cookies (continued)
38
9-38 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley - CGI was once a good way to create animation, but now there are several better ways - There are two ways to use CGI to create animation neither of which requires user intervention 1. Client-pull animation - The client repeatedly requests images from the server, which it displays in sequence - Problems: Internet is not fast enough, and if the approach were widely used, it would pull down the speed of the whole Internet 2. Server-push animation - The server sends the sequence of images to the client, with delays between them - Problems: Also creates a huge load on the Internet, and it is supported only by Netscape 9.7 Animation Using CGI
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.