Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.

Slides:



Advertisements
Similar presentations
1. XP 2 * The Web is a collection of files that reside on computers, called Web servers. * Web servers are connected to each other through the Internet.
Advertisements

© 2002 D & D Enterprises 1 Linking Images For Navigation & Clickable Image Maps.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 2 Getting Started.
Copyright © 2003 Pearson Education, Inc. Slide 1-1 The Web Wizards Guide to PHP by David A. Lash.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 1 Introduction to Perl and CGI.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 The Basics of Javascript Programming the World Wide Web Fourth.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 4 Working with the Web.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Tables Tables provide a means of organising the layout of data
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
The World Wide Web. 2 The Web is an infrastructure of distributed information combined with software that uses networks as a vehicle to exchange that.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
1/16 Steven Leung Introduction to HTML/CGI/JavaScript Intro to HTML/CGI/JavaScript How the Web Works HTML: Basic Concept CGI: Basic Concept JavaScript:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Servlets and a little bit of Web Services Russell Beale.
COS 381 Day 22. Agenda  Assignment #5 Corrected 2 B’s, 1 C, and 3 D’s  Next Capstone progress report due April 21  Capstone projects are DUE May 10.
COS 381 Day 20. Agenda  Assignment #5 Due  Capstone Progress Reports Overdue Were Due April 7  Capstone projects are DUE May 10 at 1PM  Assignment.
COS 381 Day 23. Agenda  Questions??  Resources Source Code Available for examples in Text Book in Blackboard
Chapter 10 Managing State Information PHP Programming with MySQL.
Guide To UNIX Using Linux Third Edition
COS 381 Day 23. Agenda  Questions??  Resources Source Code Available for examples in Text Book in Blackboard
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
Chapter 16 The World Wide Web. 2 Chapter Goals Compare and contrast the Internet and the World Wide Web Describe general Web processing Write basic HTML.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
Chapter 16 The World Wide Web Chapter Goals Compare and contrast the Internet and the World Wide Web Describe general Web processing Describe several.
Chapter 16 The World Wide Web. 2 The Web An infrastructure of information combined and the network software used to access it Web page A document that.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
JavaScript, Fourth Edition
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
USING PERL FOR CGI PROGRAMMING
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
Chapter 6 Server-side Programming: Java Servlets
Chapter 10 © 2003 by Addison Wesley Longman, Inc. 1 Chapter 10 Using Perl for CGI Programming.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
Chapter 5 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 5 Sebesta: Programming the World Wide Web.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
WWW and HTTP King Fahd University of Petroleum & Minerals
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Chapter 27 WWW and HTTP.
10.1 The Common Gateway Interface
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming

9-2 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

9-3 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

9-4 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

9-5 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

9-6 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

9-7 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

9-8 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 +

9-9 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

9-10 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 Perls 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

9-11 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

9-12 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

9-13 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

9-14 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

9-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.4 Accessing Form Data The param function takes a name as an argument The function returns the value associated to the name, if any, in the request

9-16 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, 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

9-17 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

9-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.5 Saving the Data A file is used to store a summary of the data The file is updated by conelec1.cgi after each survey is submitted Care must be taken that two simultaneous requests do not interfere with each other Both CGI programs will use the Perl flock function that will allow only one program at a time access to the file The file is accessed by coneclec2.cgi for each request for a summary

9-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.5 Table Shortcut Functions The Tr function will create a sequence of table rows from an argument that is a reference to a list Tr is capitalized to distinguish it from the transliterate function tr Similarly, the th and td functions will produce a sequence of tags from a reference to a list

9-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.5 Table Example Note the period between the th and td function calls, this concatenates the results in order to make a single row table({-border => "border"}, caption("Sales Figures"), Tr( [th(["Salesperson", "Mon", "Tues", Wed", "Thu", "Fri"]), ] ) );

9-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9.5 Table Example Results

9-22 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

9-23 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

9-24 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