Why CGI


We all know the great changes the WWW technology brought to us after it came into the public domain, especially after the appearance of the graphical Web browsers such as Mosaic and Netscape Navigator. Graphical Web browsers make it easier than ever before to retrieve Internet resources. You can activate a hyperlink, download a file, or read a Usenet news article with a single mouse click. This is why WWW became more and more popular.

Static Document

Web documents are usually written in HTML, a simple markup language that attempts to describe the logical structure of the text. Most often, HTML is stored in static files on the server's hard drive. Static documents are typically delivered from the file system of the Web server. The Web server software locates the file on the hard drive, opens it directly, and delivers it to the client (browser).

Now let's look at a simple static web page "example1.html", here is the HTML code:

 
 <html>
 <head ><title>Static page example</title></head>
 <center> 
 <h1>Static Page Example</h1>
 </center>
 <p>
 This is an static web page example. Each time you request this page, the Web server will
 locate this page ( whose name is "example1.html") in the hard disk and send 
 it directly back to the client. The exact same document is delivered with each request so you
 get the same content each time.  
 <center>
 <a href="whycgi.html">Back </a>
 </center>
 </html>
Click here to run the example to see the result. Each time you click, you get same information.

Dynamic Document

Static documents are straightforward and retrieved quickly. But their limitation is very clear - they can not provide dynamic information, which changes over time. This information may include accepting purchase information fromthe user, providing a search facility to a database, automatic online registration and so on. In this case, CGI provide us a terrific way to achieve these objectives. CGI makes interactive "talk" between user and server possible. By creating the document on the fly , CGI dramatically changes the world of user-computer interaction.

Now let's take a look at second example "example2.shtml" which includes CGI script. This is a dynamic but not interactive CGI example. Here is the HTML code:

 
 <html>
 <head ><title>Non-Interactive CGI Example</title></head>
 <center> 
 <h1>Non-Interactive CGI Example</h1>
 <p>
 This is an non-interactive CGI script example. It is a textclock. Each time you request 
 this page, you will get the current date and time:
 <!--#exec cgi="textclock.cgi" -->
 <center>
 <a href="whycgi.html">Back </a>
 </center>
 </html>
Click here to Run the example. You can easily see that each time you click, you will get a different time.

Interactive Communication

Using CGI, users can interactively communicate with servers. This is probably the main reason why CGI is here.
Let's see the third example, which is the famous "Hello, somebody!!" example. The following is the "example3.html":
 
 <html>
 <head ><title>Interactive CGI Example</title></head>
 <center> 
 <h1>Interactive CGI Example</h1>
 </center>
 <p> This is an interactive CGI program, which is the well-known "hello, somebody!" program. Enter
your name and click the submit button, see what will happen!
 <p> 
 <form method=POST action="cgi-bin/hello.cgi">
 <pre>
   Your Name:   <INPUT NAME="name" TYPE="TEXT" SIZE="20" MAXLENGTH="30">
   <INPUT TYPE="SUBMIT" VALUE="Send">     <INPUT TYPE="RESET">
</PRE>
 
 <center><a href="whycgi.html">Back </a></center>
 </html>
Click here to Run the example. Take a look at example1, 2 and 3. You can easily tell the difference between them. Actually, CGI works in many places where many of us may not have beenaware of it. Have you ever used a powerful search engine such as Yahoo, Infoseek or Excite ? That is CGI at work. However, while the uses of CGI can be quite powerful and far-reaching, it is not as difficult to code as one might expect. We will see some simple examples of CGI scripting as we continue with this workshop.
Previous PageTable of ContentNext Page