HTML

HTML

Animation

ActiveX

JAVA

VRML

CGI

ISAPI

Security

Grafman




Scripting | Example | Drawbacks

CGI Overview

The Common Gateway Interface (CGI) is the platform-independent standard for adding form support to Web servers. While CGI is capable of doing much more (such as building custom web pages on the fly), form handling is its primary use.

CGI was designed based on the fact that most operating systems can execute interpretive scripts, and that those scripts can interact with the operating system's environment variables and standard input and output (stdio) mechanisms. Special exceptions are made for systems like MacOS.

CGI uses standard input (stdin) to pass form requests to form handler; environment variables are used to pass certain HTTP-related information to the handler; standard output (stdout) is used by the form handler to return a response back to the requesting Web browser.


Scripting Languages

Most operating systems support some sort of simple, command-based scripting language. DOS supports BATch file scripts, NT support CMD scripts and unix supports various forms of c, bourne, korn shell scripts. All are relatively simple for most people to create and modify. Most CGI implementations support native shell scripting.

However, most of these shell scripts are limited in capability or not readily available on major platforms. One scripting language became an early favorite for CGI use: PERL.

PERL is available on most platforms, is c-like, and comes with extensive system support, socket, math, memory and string handling capabilities. Many CGI form handlers are written in PERL.

Other contenders have been TCL, Python, and most recently Java.

While both TCL and Python have some advantages over PERL, they have not gained wide acceptance as standard CGI languages. Java has gained wide acceptance, but it's not as simple for most people to learn, and requires compilation -- which complicates remote administration on some operating systems.

As a result, there will continue to be a need for something like PERL for maintaining "quick and dirty" form handlers.


PERL Form Handler Example

Here is the HTML source for the following CGI form:
    <FORM METHOD="POST" ACTION="/cgi-bin/example.pl">

    <INPUT TYPE="HIDDEN" NAME="title" VALUE="PERL Example">

    Platform:<BR>
    <INPUT TYPE="RADIO" NAME="platform" VALUE="Windows"> Windows
    <INPUT TYPE="RADIO" NAME="platform" VALUE="Unix"> Unix
    <INPUT TYPE="RADIO" NAME="platform" VALUE="MacOS" CHECKED> MacOS
    <P>

    <INPUT TYPE="TEXT" NAME="name" SIZE="25"> Your name
    <BR>
    <INPUT TYPE="PASSWORD" NAME="password" SIZE="25"> Password
    <P>

    You access the internet at:<BR>
    <INPUT TYPE="CHECKBOX" NAME="home" VALUE="home"> Home
    <INPUT TYPE="CHECKBOX" NAME="work" VALUE="work" CHECKED> Work
    <P>

    Your hair color is:
    <SELECT NAME="hair">
    <OPTION>Black
    <OPTION SELECTED>Brunette
    <OPTION>Red
    <OPTION>Blonde
    <OPTION>Other
    </SELECT>
    <P>

    Comment:<BR>
    <TEXTAREA NAME="comment" ROWS="5" COLS="30">
    Enter your comment here...
    </TEXTAREA>
    &l;P>

    <INPUT TYPE="SUBMIT" VALUE="Test PERL Script">
    <INPUT TYPE="RESET" VALUE="Reset Form">
    <P>

    </FORM>

Here is the resulting form:
    Platform:
    Windows Unix MacOS

    Your name
    Password

    You access the internet at:
    Home Work

    Your hair color is:

    Comment:

Here is the source for the PERL CGI Form Handler:
    # This is a simple PERL CGI Form handler

    require( "cgi_lib.pl" );
    &CGI_Init;

    print( "Content-type: text/html\n\n" );

    print( "<HTML>\n" );
    print( "<HEAD><TITLE>CGI PERL Example</TITLE>\n" );
    print( "<BODY>\n" );

    print( "<TABLE>\n" );
    print( "<TR><TD>User: <TD>$CGI{name}\n" );
    print( "<TR><TD>Password: <TD>You already know your password!\n" );
    print( "<TR><TD>Hair color: <TD>$CGI{hair}\n" );
    print( "<TR><TD>Platform: <TD>$CGI{platform}\n" );
    print( "<TR><TD>Browser: <TD>$ENV{HTTP_USER_AGENT}\n" );
    print( "<TR><TD>Access from: <TD>$CGI{home} $CGI{work}\n" );
    print( "</TABLE><P>\n" );

    print( "Your comments:<BR>\n" );
    print( "<UL>$CGI{comment}</UL>\n" );

    print "</BODY></HTML>\n";


Drawbacks to PERL

The major drawback to most PERL implementations is that PERL scripts need to be loaded into memory and interpretted each time they are run, which creates a serious load on Web servers.

Server-side Java handlers ease the load by being pre-tokenized. This, combined with Java's multi-threading, object-orientedness, rich class library and portability make it an increasingly popular replacement for PERL.

While compiled binary executables are non-portable, they are also becoming increasingly popular, as they generally load and run much faster than Java applets.

On NT, the Internet Server API (ISAPI) is becoming a very popular substitute for CGI. Instead of using environment variables and stdio, ISAPI uses direct memory variables and accesses TCP sockets directly. Instead of being interpretted, they are precompiled. Instead of being loaded each time they are called, they can be pre-loaded in memory as Dynamic Link Libraries (DLLs) and used "in-process".


Summary

While many Web administrators and content creators are relying increasingly on server-side Java and ISAPI, the ease of remote maintainance of PERL keeps it a serious contender for handling CGI forms.


Top | Scripting | Example | Drawbacks

© Copyright 1996 - Grafman Productions - ALL RIGHTS RESERVED
Grafman Productions