Server-side computing Why server-side? Approaches 1 Why server-side? Markup languages cannot Specify Computations Interactions with users Provide access to Server-side resources Databases Programs Services 2
Approaches programmer centered designer centered 3 programmer centered Document generation inside computation code CGI Java servlets 4
designer centered Computation code inside document template PHP ASP JSP 5 CGI: the Common Gateway Intefrace Let browsers request server-side execution Open source LAMP Linux, Apache, MySQL, Px Px Perl, PHP, Python 6
ASP or ASP.NET: Active Server Pages Microsoft 7 JSP: Java Server Pages Similar to ASP and ASP.NET Java-based Sun Microsystems 8
CGI Interface between browser and server HTTP request ==> Program instead of document Server recognizes by file Location Special directories Extension 9 CGI program Often stored in cgi-bin directory Produce complete HTTP response, or just URL of existing doc 10
CGI linkage Some CGI programs in machine code Perl programs usually kept in source form ==> must be run perl on them 11 source file can be made executable add line at beginning specifies language processing run first 12
For Perl programs if perl in /usr/local/bin/perl often in UNIX ==> #!/usr/local/bin/perl -w 13 for Perl CGI programs sometimes use file extension.cgi e.g., for the CGI Wrap system 14
HTML document specifies CGI program with hypertext reference attribute href, of anchor tag, <a> as in <a href =./cgi-bin/reply.cgi>" Click here to run the CGI program, reply.pl </a> 15 Communications and computation using CGI 16
Model... Browser server application Form Submit completed form Call CGI CGI CGI program's response CGI program's response 17 18
<!-- reply.html - calls a trivial cgi program --> <html> <head> <title> HTML to call the CGI-Perl program reply.cgi </title> </head> <body> This is our first CGI-Perl example <a href = "./cgi-bin/reply.cgi"> Click here to run the CGI program, reply.cgi </a> </body> </html> 19 CGI --> browser connection from CGI program back to requesting browser Through STDOUT Through server HTTP header needs only content type followed by blank line print "Content-type: text/html \n\n"; SHOW reply.cgi 20
Query string names and values of widgets values always coded as strings format of name/value pair name=value If form has more than one widget ==> values separated with ampersands milk=2&payment=visa special characters coded percent sign + two-character hexadecimal number = ASCII code of character Some browsers code spaces as + signs rather than as %20 get versus post HTTP methods 21 GET vs. POST... GET + Access CGI prog. w/ query without a form Pass parameters to program... Can send extra path info... Query might get truncated Post + Unlimited query length No canned queries (hard-coded url) 22
The CGI.pm Module - A Perl module serves as a library - Can be used as a function library or a class library - Get info about by typing perldoc CGI - The Perl use declaration is used to make a module available to a program - To make only part of a module available, specify the part name after a colon (For our purposes, only the standard part of the CGI module is needed) use CGI ":standard"; - Common Functions 23 The CGI.pm Module Perl module serves as library Can be used as function library or class library Info: perldoc CGI 24
Perl use declaration make module available to program to make only part of module available specify part name after colon For our purposes only need standard part of CGI module use CGI ":standard"; 25 Common CGI.pm Functions Shortcut functions produce tags using their parameters as attribute values br; returns "<br/>" 26
e.g., h2("very easy!"); returns <h2> Very easy! </h2> parameter to function h2 used as content of <h2> tag 27 function output to return doc call must be parameter to print print h2("very easy!"); 28
Tags can have both content and attributes Each attribute passed as name/value pair just as in hash literal Attribute names passed with preceding dash 29 textarea(-name => "Description", -rows => "2", -cols => "35" ); Produces: <textarea name ="Description" rows=2 cols=35> </textarea> 30
If both content and attributes passed to function attributes specified in hash literal as first parameter a({-href => "fruit.html"}, "Press here for fruit descriptions"); Output: <a href="fruit.html"> Press here for fruit descriptions</a> 31 Tags and their attributes distributed over parameters of function ol(li({-type => "square"}, ["milk", "bread", "cheese"])); Output: <ol> <li type="square >milk</li> <li type="square >bread</li> <li type="square >cheese</li> </ol> 32
CGI.pm also includes non-shortcut functions produce output for return to user call to header() produces: Content-type: text/html;charset=iso- 8859-1 -- blank line -- 33 The start_html function create head of return document <body> tag parameter to start_html used as title of document start_html("bill s Bags"); 34
DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0 //EN" "http://www.w3.org/tr/xhtml-basic/ xhtml-basic10.dtd"> <html xmlns= "http://www.w3.org/1999/xhtml lang="en-us"> <head><title>bill s Bags</title> </head> <body> 35 The param function given a widget s name returns widget s value If, e.g., query string has name=abraham in it, param("name") will return "Abraham" 36
The end_html function generates </body></html> SHOW popcorn.html display popcorn.cgi 37 FIGURE 10.2 Display of popcorn.html 38
FIGURE 10.3 Display of popcorn.html, form filled out 39 FIGURE 10.4 Output of popcorn.cgi 40
A Survey Example - Use form to collect survey data from users - accumulate survey results, stored between form submissions in file on server 41 concurrent use of file ==> must be protected from corruption block other accesses while being updated Under UNIX, Perl function flock 42
parameter values for flock defined in Perl Fcntl module (write lock is LOCK_EX) To update file open file lock with flock modify rewind with seek close file --> SHOW conelec.html + display 43 Two CGI programs used 1. collect survey submissions and record new data 2. produce current totals file format eight lines each w/ 7 values first four for female responses last four for male responses 44
FIGURE 10.5 Display of conelec.html 45 To collect and record form data must: 1. Get form values (with param) 2. Determine row of file to be modified 3. Open, lock, and read survey data file 4. Split affected data string into numbers + store them in array 46
5. Modify affected array element & join array back into string (use gender to determine which half of data affected) 6. Rewind data file (with seek) 7. Rewrite and close survey data file --> SHOW conelec1.pl 47 Tables easier to specify with CGI.pm - table created with table function - border attribute specified as parameter - table s caption created with call to caption, as second parameter to table 48
- Each row of table is created with call to Tr - heading row created with call to th - Data cells created with calls to td - calls to Tr, th, and td require references as parameters 49 - Suppose have three arrays of sales numbers - one for each of three salespersons - each array has one value for each day of work week - want to build table of this information - using CGI.pm 50
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), ] ) ); 51 program that produces current results must: 1. Open file and read lines into array of strings 2. Split first four rows (responses from females) into arrays of votes for four age groups 3. Unshift row titles into vote rows (making them first elements) 4. Create column titles row with th put its address in array 52
5. Use td on each rows of votes 6. Push addresses of rows of votes onto row address array 7. Create table using Tr on array of row addresses 8. Repeat Steps 2-7 for last four rows of data (responses from males) 53 --> SHOW conelec2.cgi --> SHOW Figure 10.7 54
FIGURE 10.7 Survey results page 55 10.6 Cookies - session = time span during which browser interacts with particular server - HTTP protocol is stateless - But, several reasons why useful for server to relate request to session - Shopping carts for many different simultaneous customers - Customer profiling for advertising - Customized interfaces for specific clients 56
Approaches to storing client information: - Store on server - too much to store! - Store on client machine - this works 57 cookie = small object of information name and text value created by some software system on server (maybe CGI program) Every HTTP communication between browser and server includes information in header about message when cookie created given lifetime Every time browser sends request to server that created cookie, while cookie is still alive cookie included 58
browser can be set to reject cookies - CGI.pm includes support for cookies cookie(-name => a_cookie_name, -value => a_value, -expires => a_time_value); - name can be any string - value can be any scalar value - time is a number followed by a unit code (d, s, m, h, M, y) 59 Cookies must be placed in HTTP header when header created header(-cookie => $my_cookie); To fetch cookies from HTTP request call cookie with no parameters hash of all current cookies returned To fetch value of one particular cookie send cookie s name to cookie function $age = cookie( age ); 60
Example: cookie that tells customer time of last visit to this site Use Perl function, localtime to get parts of time ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime; SHOW time_date.pl 61 CGI program to solve our problem: 1. Get cookie named last_time 2. Get current day of week month day of month & put them in cookie named last_time 3. Put cookie in header of return document 62
4. If no existing cookie produce welcome message for first-time visitor 5. If there was cookie produce welcome message includes previous day of week month day of month SHOW day_cookie.pl 63