Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

14
Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18

Transcript of Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Page 1: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Web Programmingfor DB Applications

Yuen-Hsien Tseng

2006/04/18

Page 2: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Web Environment• Browser: IE, Firefox, …• Web Server: Apache, IIS, …• DBMS: Access, MS SQL, MySQL, Oracle, …• Web Programming Env.: CGI, ASP, JSP, PH

P

Web Server

DBMS/ DB server

P1

P2

Pn

CGI

ODBC ASP PC

Browser

DB Client

Internet

Server

Page 3: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Web Server Administration

• Virtual Path: – Root dir: Where is http://localhost/ in file system

• Default index page: index.htm[l], welcome.htm, …

– User dir: How http://localhost/~sam/ maps to d:/sam/public_html

– How http://localhost/demo/ maps to d:/demo

• Executable and non-executable files– Configurations and File permissions– http://www.cgi101.com/book/connect/winxp.html

• Port number

Page 4: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Apache Configuration

• An example configuration to run a program:

Alias /~tseng/ "D:/Sam/public_html/"

<Directory "D:/Sam/public_html">

Options Indexes MultiViews ExecCGI

AllowOverride AuthConfig

Order allow,deny

Allow from all

</Directory>

Page 5: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

CGI (Common Gateway Interface)

• Messages sent to Web server– print “Content type: text/html\n\n<html>…”;

• Message received from Web server– URL-encoding/decoding– method = Get or Post

• http://localhost/test.pl?name=sam&age=40

<form action=“test.pl” method=Post>

Name: <input type=text name=“name” value=“sam”>

Age: <input type=text name=“age” value=“40”>

<input type=submit value=“Hit me!”>

</form>

Page 6: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Introduction to Perl

• Invented by Larry Wall in late 1980’s

• Combined the merits of many languages– C/C++, Unix Shell, …– String Processing, Regular Expression, …

• Suitable for– File management– String manipulation– Process control

• Lot of resources in source codes– http://www.cpan.org/

Page 7: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Perl Basics

• Data Types– Scalar (numeric, strings), array, hash, reference

• Flow control– If elsif else, for, foreach, while, next, last

• Special variables

• Subroutines

• Regular Expressions

• Reference and Object-oriented programming

Page 8: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Data types• Scalar

– $a=1; $b=1.2; $a = $a . $b . “=sam”;

• Array– @C=(1, “e”, 3.4); $c=$C[1]; @a = @C[0..1]; – $a = pop @C; $b= unshift @C; shift @C, ‘c’; push @a, ‘e’;

• Hash– %H=(‘sam’, 4, ‘joe’, 2); %G=(‘sam’=>4, ‘joe’=>2);– %T=reverse %H;

• Reference– $b=\@C; $a=$b->[0];– $a=\%H; $b=($a->{‘sam’} == 4)?’Yes’:”no”;

• http://www.cgi101.com/book/ch2/text.html

Page 9: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Flow control

foreach (@A) {

next if /^\d/; # next unless not /^\d/;

last if /^\s*$/;

print ;

}

for($i=0; $i<$n; $i+=2) { print $A[$i]; }

While (<condition>) { … }

Page 10: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Special Variables

• $_, @_,

• @ARGV

• Sort { $b eq $a } keys %H;

• Sort { $H{$b} <=> $H{$a} } keys %H;

Page 11: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Subroutines

($r, $i) = &add($r1, $i1, $r2, $i2, “no”, “use”);

sub add {

my($r1, $i1, $r2, $i2, @r) = @_;

return ($r1+$r2, $i2+$i2);

}

Page 12: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

Regular Expressions• print "It matches\n" if "Hello World" =~ /World/;

– $greeting = "World"; – print "It matches\n" if "Hello World" =~ /$greeting/;

• "Hello World" =~ /world/; # doesn't match, case sensitive • "Hello World" =~ /o W/; # matches, ' ' is an ordinary char • "Hello World" =~ /World /; # doesn't match, no ' ' at end • Metacharacters : {}[]()^$.|*+?\

– "2+2=4" =~ /2+2/; # doesn't match, + is a metacharacter– "2+2=4" =~ /2\+2/; # matches, \+ is treated like an ordinary +– /[yY][eE][sS]/; # match 'yes' in a case-insensitive way

• # 'yes', 'Yes', 'YES', etc.

– /yes/i; # also match 'yes' in a case-insensitive way– /item[0-9]/; # matches 'item0' or ... or 'item9'– /[0-9a-fA-F]/; # matches a hexadecimal digit

• $time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format– $hours = $1; $minutes = $2; $seconds = $3;

Page 13: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

More tutorials

• Reference– See c:/Perl/html/lib/Pod/perlreftut.html

• Object-oriented programming– See c:/Perl/html/lib/Pod/perltoot.html

Page 14: Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.

CGI Module and Environment Variables

• See – http://www.cgi101.com/book/ch3/text.html– c:/Perl/html/lib/CGI.html

• Use CGI; print param(‘name’);

• print "Caller = $ENV{HTTP_REFERER}\n";