980 likes | 1.21k Views
Internet Engineering Course. Web Application Development. Introduction. Company needs to provide various web services Hosting intranet applications Company web site Various internet applications Therefore there is a need to develop applications We should select web application model to use
E N D
Internet Engineering Course Web Application Development
Introduction • Company needs to provide various web services • Hosting intranet applications • Company web site • Various internet applications • Therefore there is a need to develop applications • We should select web application model to use • Discuss on different types of applications
Content • Application development models: • SSI • CGI with Perl • LAMP/WAMP • J2EE • .Net • Typical web applications in an organization • CMS/DMS • Groupware (Collaboration software) • WIKI • Workflow
SSI • Server Side Includes • This is very simple model, not really an application development model suitable for middle to large size applications • Web server processes such instructions and generate dynamic content • Directives that are placed in HTML pages, and evaluated on the server while the pages are being served. • Let you add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program, or other dynamic technology.
SSI • SSI directives have the following syntax: <!--#element attribute=value attribute=value ... --> • It is formatted like an HTML comment • so if you don't have SSI correctly enabled, the browser will ignore it
SSI examples • Commonly used to obtain and display environment variables from the OS: <!--#echo var="DATE_LOCAL" --> • Modification date of the file This document last modified <!--#flastmod file="index.html" --> • Including the results of a CGI program: <!--#include virtual="/cgi-bin/counter.pl" --> • Including a standard footer: <!--#include virtual="/footer.html" -->
SSI examples (cont.) • Executing commands: <pre> <!--#exec cmd="ls" --> </pre> • Conditional expressions: <!--#if expr="${Mac} && ${InternetExplorer}" --> Apologetic text goes here <!--#else --> Cool JavaScript code goes here <!--#endif -->
SSI conclusion • Minimize code duplications • SSI for site management • Creates dynamic content • Can be a security problem
CGI • Common Gateway Interface • Invented in 1993 by NCSA for HTTPd web server • Client requests program to be run on server-side • Web server passes parameters to program through UNIX shell environment variables • Program spawned as separate process via fork • Program's output => Results • Server passes back results (usually in form of HTML) • Good for interfacing external applications with information servers • it is language independent • CGI programs are most often written in PERL, C/C++, VB, Java, or UNIX shell scripts.
CGI Request service Run CGI program … … … print $result HEADERS BODY
CGI with Perl • Write a standard Perl Program • Program's output (to stdout) is sent back as HTTP Response • You must write out everything • Headers • Blank Space • Body
CGI with Perl • Some CGI programs are in machine code, but Perl programs are usually kept in source form, so Perl must be run on them • A source file can be made to be “executable” by adding a line at their beginning that specifies that a language processing program be run on them first • For Perl programs, if the perl system is stored in /usr/local/bin/perl, as is often is in UNIX systems, this is: • #!/usr/local/bin/perl • The file extension .cgi is sometimes used for Perl CGI programs • An HTML document specifies a CGI program with the hypertext reference attribute, href, of an anchor tag, <a>, as in • <a href = “./cgi-bin/reply.cgi>" Click here to run the CGI program, reply.pl </a>
Perl • Practical Extension and Reporting Language. • Originally developed as a utility language for UNIX. • Particularly well suited to manipulate patterns, especially text. • Popular because it is free, available for most operating systems, and relatively easy to learn • Exceedingly powerful, but noisy, and prone to errors.
Perl – a simple example • “Hello World” in Perl #! /usr/bin/perl print "Content-type: text/html\n\n"; print "<html><body><h1>Hello World!"; print "</h1></body></html>\n"; • Simple concept -- the program executes, and the output is sent to the browser that called it.
Perl – a simple counter #! /usr/bin/perl open (INPUT,”count.txt”); @inline= <INPUT>; $count = $inline[0] + 1; close INPUT; open (OUT,”>count.txt”); print OUT “$count\n”; close OUT; print "Content-type: text/html\n\n"; print "<html><body>”; print “<h1>Let’s Count! "</h1>"; print “This page accessed $count times<p>”; print “</body></html>\n";
Perl – Basic syntax • Perl statements end in a semi-colon: • Comments start with a hash symbol and run to the end of the line • Whitespace is irrelevant:
Perl – Basic syntax (cont.) • Variable types: • Scalars: • Arrays:
Perl – Basic syntax (cont.) • Variable types: • Arrays:
Perl – Basic syntax (cont.) • Variable types: • Hashes:
Perl – Basic syntax (cont.) • Variable scoping: • Conditional constructs:
Perl – Basic syntax (cont.) • Conditional constructs: • While:
Perl – Basic syntax (cont.) • for: • foreach:
Perl – Basic syntax (cont.) • Operators: • Arithmetic: • Numeric comparison:
Perl – Basic syntax (cont.) • Operators: • String comparison: • Boolean logic:
Perl – Basic syntax (cont.) • Files and IO:
Perl – Basic syntax (cont.) • String matching:
Perl – Basic syntax (cont.) • String matching:
Perl – Basic syntax (cont.) • Subroutines:
CGI environment variables (%ENV) • example- Printing environment variables: #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowserfatalsToBrowser); print header; print start_html("Environment"); foreach my $key (sort(keys(%ENV))) { print "$key = $ENV{$key}<br>\n"; } print end_html;
CGI environment variables (%ENV) • Example- Referrer: #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowserfatalsToBrowser); print header; print start_html("Referring Page"); print "Welcome, I see you've just come from $ENV{HTTP_REFERER}!<p>\n"; print end_html;
CGI environment variables (%ENV) • Example- Browser detection: #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowserfatalsToBrowser); print start_html("Browser Detect"); my($ua) = $ENV{HTTP_USER_AGENT}; print "User-agent: $ua<p>\n"; if (index($ua, "MSIE") > -1) { print "Your browser is Internet Explorer.<p>\n"; } elsif (index($ua, "Netscape") > -1) { print "Your browser is Netscape.<p>\n"; } elsif (index($ua, "Safari") > -1) { print "Your browser is Safari.<p>\n"; } elsif (index($ua, "Opera") > -1) { print "Your browser is Opera.<p>\n"; } elsif (index($ua, "Mozilla") > -1) { print "Your browser is probably Mozilla.<p>\n"; } else { print "I give up, I can't tell what browser you're using!<p>\n"; } print end_html;
Form processing (cont.) #!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header; print start_html("Thank You"); print h2("Thank You"); my %form; foreach my $p (param()) { $form{$p} = param($p); print "$p = $form{$p}<br>\n"; } print end_html;
Form processing (cont.) #!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header; print start_html("Results"); # Set the PATH environment variable to the same path # where sendmail is located: $ENV{PATH} = "/usr/sbin"; # open the pipe to sendmail open (MAIL, "|/usr/sbin/sendmail -oi -t") or &dienice("Can't fork for sendmail: $!\n"); # change this to your own e-mail address my $recipient = 'nullbox@cgi101.com';
Form processing (cont.) # Start printing the mail headers # You must specify who it's to, or it won't be delivered: print MAIL "To: $recipient\n"; # From should probably be the webserver. print MAIL "From: nobody\@cgi101.com\n"; # print a subject line so you know it's from your form cgi. print MAIL "Subject: Form Data\n\n"; # Now print the body of your mail message. foreach my $p (param()) { print MAIL "$p = ", param($p), "\n"; } # Be sure to close the MAIL input stream so that the # message actually gets mailed. close(MAIL);
Form processing (cont.) # Now print a thank-you page print <<EndHTML; <h2>Thank You</h2> <p>Thank you for writing!</p> <p>Return to our <a href="index.html">home page</a>.</p> EndHTML print end_html; # The dienice subroutine handles errors. sub dienice { my($errmsg) = @_; print "<h2>Error</h2>\n"; print "<p>$errmsg</p>\n"; print end_html; exit; }
Setting cookies #!/usr/bin/perl use strict; my $cid = int(rand(1000000)); print "Set-Cookie: NAME=$cid\n"; print "Content-type: text/html\n\n"; print<<EndOfHTML; <html><head><title>Welcome</title></head><body><h2>Welcome!</h2>Your cookie is $cid.<p></body></html> EndOfHTML ;
Reading cookies #!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header(); print start_html("Cookie"); print h2("Welcome!"); if (my $cookie = cookie('mycookie')) { print "Your cookie is $cookie.<br>"; } else { print "You don't have a cookie named `mycookie'.<br>"; } print end_html;
References • http://httpd.apache.org/docs/1.3/howto/ssi.html • http://learn.perl.org/ • http://www.stanford.edu/class/cs193i/handoutsSum2004/21CGI.pdf • http://www.stanford.edu/class/cs193i/handoutsSum2004/22CGI2.pdf • http://www.stanford.edu/class/cs193i/handoutsSum2004/23CGI3.pdf
What is LAMP? • LAMP refers to a set of tools: • Linux • Apache • MySQL • PHP • It allows for rapid deployment of software applications • It can be defined as Open Source platform • We have already discussed on Linux and Apache • We should talk more about PHP and MySQL
PHP overview • Open Source server-side scripting language designed specifically for the web. • In-line scripting • Conceived in 1994, now used on +10 million web sites. Now in version 5.0 • Outputs not only HTML but can output XML, images (JPG & PNG), PDF files and even Flash movies (using libswf and Ming) all generated on the fly. Can write these files to the filesystem. • Supports a wide-range of databases (inherently or via ODBC). • PHP also has support for talking to other services using protocols such as LDAP, IMAP, SNMP, POP3, HTTP. • Supports OO programming • Perl- and C-like syntax. Relatively easy to learn. • Website @ http://www.php.net/
Why use PHP • If you like free software or need a free solution • If you need a solution that’s portable across multiple platforms (e.g. Red Hat Linux to Windows 2000) • If you want to add dynamic content to your pages • If you want to make your pages easier to maintain • There are a lot of open source/free packages/libraries available in PHP. • Many mailing lists/sites are dedicated to it. • Examples of uses of PHP : • Small/Medium Portals • Small/Medium Web-Mails • Content Management
What is in a php file • PHP files may contain text, HTML tags and scripts • PHP files are returned to the browser as plain HTML • PHP files have a file extension of ".php", ".php3", or “.phtml“ • Embedding PHP in HTML: <html> <body> <strong>Hello World!</strong><br /> <? echo ‘This is a PHP introductory course!’; ?> </body> </html>
Include mechanism <?php include '../includes/header.html'; ?> <center> content of your web page </center> <?php include 'http://cs.ucy.ac.cy/php/footer.html'; ?> • Content can be included from a local or remote source via such protocols as HTTP, HTTPS, FTP, and FTPS
Types • Scalar types • Boolean • Integer • Float • String • Compound types • Array • Object
Variables • Variables all start with a $ • Case-sensitive • Must start with a letter or underscore, followed by any number of letters, numbers, or underscores • Variables are not explicitly typed • Type of value is inferred on operator application • Uninitialized variables have value undef • What undefmeans depends on context • Numeric context it is 0 • String context it is empty string “”
Variables • To assign values to variables: • $foo = ‘bar’; Data Type: String • $foo = 1; Data Type: integer • $foo = 5.34; Data Type: float • $foo = array(“bar”,”united”); Data Type: Array • Data Types are automatically assigned though you can force a data type by type casting. For example: • $foo = ‘Hello’; • $bar = (int)$foo; • $bar now equals 0 • Almost all variables are local (page). Global variables include $_Session
Example <html> <body> <p> <?php $temperature = 5; $conversionFactorC2K = 273; print("$temperature °C"); echo " is "; print($temperature+$conversionFactorC2K."°K"); ?> </p> </body> </html>