您的位置:寻梦网首页编程乐园CGI编程Zhanshou's CGI Tutorial

Why CGI


Almost every one knows the great changes that 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. Graphic 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 "staticPage.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 "staticPage.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#static">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 obvious - they can not provide dynamic information, which changes over time. Those informations may include accepting purchase information from users, providing search facilities to databases, automatic online registrations and so on. In those cases, server-side technologies like CGI provide us a good solution. They make interactive "talk" between client and server possible. By creating the document on the fly, we can do some REAL business over the internet, even sitting at home all the time.

Now let's take a look at second example "textClock.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="cgi-bin/textclock.pl" -->
 <center>
 <a href="whycgi.html#dynamic">Back </a>
 </center>
 </html>

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 "hello.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.pl">
 <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#interactive">Back </a></center>
 </html>
Click here to Run the example. Take a look at the above three example, 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 where CGI are working. 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 Page Table of Content Next Page