CGI Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CGI Programming

Description:

CGI Programming – PowerPoint PPT presentation

Number of Views:224
Avg rating:3.0/5.0
Slides: 17
Provided by: jason294
Category:

less

Transcript and Presenter's Notes

Title: CGI Programming


1
CGI Programming
  • The Client Side and the Server Side

2
How CGI Works
  • CGI is a method allowing web pages to communicate
    with programs on an HTTP server. These programs
    can be CGI scripts, applications, or applets.
  • A request is made for a URL from a location
    reserved by the HTTP server. This location,
    usually called cgi-bin, cgi, or bin, holds cgi
    scripts that can be run to send back output.
    Data can be supplied to a CGI program by
    appending it to the URL using the GET method, or
    by attaching it to a separate line using the POST
    method.

3
CGI Cont.
  • A CGI script is a program, usually written in
    C,C, or Perl.
  • An example of a simple CGI script written in Perl
  • !/user/bin/perl
  • print gtgtEND_of_HTML
  • Content type text/html
  • lthtmlgt
  • ltheadgt
  • lttitlegtHello World in Perllt/titlegt
  • lt/headgt

4
Example in Perl Cont.
  • ltbody bgcolorFFFFFFgt
  • ltcentergtlth1gtHello, World!lt/h1gtlt/centergt
  • lt/bodygt
  • lt/htmlgt
  • END_of_HTML
  • This script is static, meaning it doesnt allow
    for any input by the user. It can only send back
    output to the user.

5
CGI Script for a Form
  • The last example could only print output to a
    browser for the user to read. This example takes
    information (your name) from a form imbedded in a
    web page that is located on a server, stores the
    information, and sends back an HTML page to the
    user.

6
Form Script Cont.
  • !/usr/local/perl -w
  • helloyou.plx is a program to tell you hello by
    name
  • set up to use the CGI.pm module
  • use CGI qw(param)
  • get your name you typed on the HTML form, using
    the CGI.pl module
  • my yourname param("yourname")
  • send the top part of the new HTML code to the
    browser
  • print gtgtEND_top
  • Content-type text/html

7
Form Script Cont.
  • ltheadgt
  • lttitlegtThe next steplt/titlegt
  • lt/headgt
  • ltbody bgcolor"FFFFFF"gt
  • ltbrgt
  • END_top
  • send hello and the name from the form to the
    browser
  • print ("lth1gtHello, yourname!lt/h1gt")
  • lthtmlgt
  • send the last part of the new HTML code to the
    browser
  • print gtgtEND_bottom
  • lt/bodygt
  • lt/htmlgt
  • END_bottom

8
HTML Form for Previous Example
  • lthtmlgt
  • ltheadgt
  • lttitlegtSet up for Hello, YOU!lt/titlegt
  • lt/headgt
  • ltbody bgcolor"FFFFFF"gt
  • ltform action"http//www.yoursite.com/cgi-bin/hell
    oyou.plx"gt
  • lth1gtEnter your name, up to 20 letterslt/h1gtltbrgt
  • ltinput type"text" name"yourname" size"20"gtltbrgt
  • ltinput type"submit"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

9
Explanation of Script
  • Within the HTML Form is a FORM action command.
    This command gives the location of the server
    with the cgi-bin that contains a proper script
    that can read and interpret the form. The server
    in this example is a fictious one. Examples
    could include such server locations as
    www.yahoo.com or www.okstate.edu. These
    servers have compilers these can include Java,
    C, C, Visual Basic, and in our example Perl.
    Depending on which language you use to program,
    you need that particular compiler.

10
Explanation Cont.
  • Once the Submit button in the form is clicked,
    the form is sent to the server. Within the FORM
    action command is the location of the folder
    containing the script. Inside this folder is the
    Perl program where the form will be interpreted.
    In our example, the folder containing the program
    was located at www.yoursite.com/cgi-bin/, and
    the program was called hellowyou.plx.
  • When the form is received, the program executes.

11
Expl. Cont.
  • The program recieves the environment variable,
    designated by the . It saves the environment
    variable as its own parameter. This is denoted
    by the code, my yourname
    param(yourname)
  • Next, a return HTML page is sent back to the
    user. The page will contain the string, Hello,
    yourname!

12
Scripting Languages
  • CGI scripts can be written in various languages.
    The most popular of these are C and Perl, with
    Java growing in popularity. CGI programs are
    called scripts because the first programs were
    written using UNIX shell scripts and Perl.
  • Perl, along with Python, a less popular scripting
    language, is an interpreted language, somewhat
    like a DOS batch file. When a Perl program is
    executed, the Perl instructions are interpreted
    and immediately compiled into machine
    instructions.

13
Languages Cont.
  • C and Java, on the other hand, is compiled ahead
    of time, and the resulting executable isnt
    normally called a script. Compiled programs
    usually run faster but are more difficult to
    modify.
  • Most major web sites run on Unix servers.
    However, Windows-based servers are becoming more
    and more prevelant.

14
Languages Cont.
  • Windows-based servers use temporary files for
    input and output, rather than environment
    variables and standard input/output as in the
    original CGI standard.
  • Variables can be accessed from the CGI programs
    written in Perl, C, C, or scripting languages
    such as Unix shell scripts, or with a one-line
    intermediate script from Java.

15
Languages Cont.
  • When writing CGI scripts in Java, an intermediate
    shell script (or .bat file if on Windows) must be
    written to pass along the value of the
    QUERY_STRING variable even if your Java compiler
    generates stand-alone executables.
  • This is because Java has no method to directly
    read environment variables due to the fact that
    this concept is foreign to some operating systems
    supported by Java.

16
Resources
  • All information in this PowerPoint can be found
    in Core Web Programming 1998 Prentice Hall PTR
    and Using HTML 4, XML, and Java 1.2 Platinum
    Edition 1999 Que.
Write a Comment
User Comments (0)
About PowerShow.com