Server-side computing • Why server-side? • Approaches
I VP R
© Copyright 2007 Haim Levkowitz
1
Why server-side? • Markup languages cannot • Specify • Computations • Interactions with users • Provide access to • Server-side resources • Databases • Programs • Services I VP R
© Copyright 2007 Haim Levkowitz
2
Approaches • “programmer centered” • “designer centered”
I VP R
© Copyright 2007 Haim Levkowitz
3
“programmer centered” • Document generation inside computation code • CGI • Java servlets
I VP R
© Copyright 2007 Haim Levkowitz
4
“designer centered” • Computation code inside document template • PHP • ASP • JSP
I VP R
© Copyright 2007 Haim Levkowitz
5
CGI: the Common Gateway Intefrace • Let browsers request server-side execution • Open source • “LAMP” • Linux, Apache, MySQL, Px • Px • Perl, PHP, Python
I VP R
© Copyright 2007 Haim Levkowitz
6
ASP or ASP.NET: Active Server Pages • Microsoft
I VP R
© Copyright 2007 Haim Levkowitz
7
JSP: Java Server Pages • Similar to ASP and ASP.NET • Java-based • Sun Microsystems
I VP R
© Copyright 2007 Haim Levkowitz
8
CGI • Interface between browser and server • HTTP request ==> • Program instead of document • Server recognizes by file • Location • Special directories • Extension I VP R
© Copyright 2007 Haim Levkowitz
9
CGI program • Often stored in cgi-bin directory • Produce • complete HTTP response, or • just URL of existing doc
I VP R
© Copyright 2007 Haim Levkowitz
10
CGI linkage • Some CGI programs in machine code • Perl programs usually kept in source form • ==> must be run perl on them
I VP R
© Copyright 2007 Haim Levkowitz
11
• source file can be made “executable” • add line at beginning • specifies language processing • run first
I VP R
© Copyright 2007 Haim Levkowitz
12
For Perl programs if perl in /usr/local/bin/perl often in UNIX ==> #!/usr/local/bin/perl -w
I VP R
© Copyright 2007 Haim Levkowitz
13
for Perl CGI programs sometimes use file extension .cgi e.g., for the CGI Wrap system
I VP R
© Copyright 2007 Haim Levkowitz
14
HTML document specifies CGI program with hypertext reference attribute href, of anchor tag, as in " Click here to run the CGI program, reply.pl
I VP R
© Copyright 2007 Haim Levkowitz
15
Communications and computation using CGI
I VP R
© Copyright 2007 Haim Levkowitz
16
Model ... • Browser
server Submit completed form
application Call CGI
CGI
Form CGI program's response
CGI program's response
I VP R
© Copyright 2007 Haim Levkowitz
17
I VP R
© Copyright 2007 Haim Levkowitz
18
HTML to call the CGI-Perl program reply.cgi This is our first CGI-Perl example Click here to run the CGI program, reply.cgi I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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)
I VP R
© Copyright 2007 Haim Levkowitz
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";
I VP R
© Copyright 2007 Haim Levkowitz
23
- Common CGI.pm Functions - “Shortcut” functions produce tags, using their parameters as attribute values br;
returns
The CGI.pm Module "
"
Perl module serves as library Can be used as function library or class library Info: perldoc CGI
I VP R
© Copyright 2007 Haim Levkowitz
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";
I VP R
© Copyright 2007 Haim Levkowitz
25
Common CGI.pm Functions “Shortcut” functions produce tags using their parameters as attribute values br; returns "
"
I VP R
© Copyright 2007 Haim Levkowitz
26
e.g., h2("Very easy!"); returns Very easy! parameter to function h2 used as content of tag
I VP R
© Copyright 2007 Haim Levkowitz
27
function output to return doc call must be parameter to print print h2("Very easy!");
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
29
textarea(-name => "Description", -rows => "2", -cols => "35" ); Produces:
I VP R
© Copyright 2007 Haim Levkowitz
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: Press here for fruit descriptions I VP R
© Copyright 2007 Haim Levkowitz
31
Tags and their attributes distributed over parameters of function ol(li({-type => "square"}, ["milk", "bread", "cheese"])); Output: bread Bill’s Bags
I VP R
© Copyright 2007 Haim Levkowitz
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"
I VP R
© Copyright 2007 Haim Levkowitz
36
The end_html function generates SHOW popcorn.html display popcorn.cgi
I VP R
© Copyright 2007 Haim Levkowitz
37
FIGURE 10.2 Display of popcorn.html
I VP R
© Copyright 2007 Haim Levkowitz
38
FIGURE 10.3 Display of popcorn.html, form filled out
I VP R
© Copyright 2007 Haim Levkowitz
39
FIGURE 10.4 Output of popcorn.cgi
I VP R
© Copyright 2007 Haim Levkowitz
40
A Survey Example - Use form to collect survey data from users - accumulate survey results, stored between form submissions in file on server
I VP R
© Copyright 2007 Haim Levkowitz
41
concurrent use of file ==> must be protected from corruption block other accesses while being updated Under UNIX, Perl function flock
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
44
FIGURE 10.5 Display of conelec.html
I VP R
© Copyright 2007 Haim Levkowitz
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 … I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
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), ] ) ); I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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) I VP R
© Copyright 2007 Haim Levkowitz
53
© Copyright 2007 Haim Levkowitz
54
--> SHOW conelec2.cgi --> SHOW Figure 10.7
I VP R
FIGURE 10.7 Survey results page
I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
56
Approaches to storing client information: - Store on server - too much to store! - Store on client machine - this works
I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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)
I VP R
© Copyright 2007 Haim Levkowitz
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′);
I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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 I VP R
© Copyright 2007 Haim Levkowitz
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
I VP R
© Copyright 2007 Haim Levkowitz
63