⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch1.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 2 页
字号:
more defined. This means that I don't have to be the first one
to figure out how to index files with a CGI program; it's already
been done. This makes the small tasks that combine to form an
advanced task, such as database handling, easier. Plus, being
older, a lot more major companies have provided ways to handle
their particular database format with a CGI program, giving you
an easy way to extrapolate the data. Second, Java is a programming
language. CGI is a specification for gateway programs. If Java
changes significantly, you have to rewrite the entire program.
If CGI changes significantly, you just have to upgrade to a new
CGI library program.
<P>
These are just some of the many advantages of advanced tasks.
Because of these advantages, CGI is still useful; however, there
are some things CGI is not useful for.
<H2><A NAME="WhatCGIIsNotUsefulFor"><FONT SIZE=5 COLOR=#FF0000>What
CGI Is Not Useful For</FONT></A></H2>
<P>
There comes a point where you can do program and app in CGI, or
you can code the app using a different language/technology/and
so on, and it is easier to code the app with something besides
CGI. One example is simple imagemapping. Because an imagemap applet
already exists in the JDK (Java Development Kit), you could, with
no work, use that to handle imagemapping. It would be a better
idea to use both methods at first, but because all major browsers
will support Java soon (from Microsoft to Netscape, and many others),
it is a better idea to program it in Java. Another example is
animation. It is infinitely easier to use the animation applet
that the JDK includes to handle animation than it is to use a
CGI script; something generated with a CGI script can't appear
in the middle of the page and have text after it, unless it uses
client push/server pull or some other method. The bottom line
is that after Java becomes standard, CGI will mainly be used to
write quick and dirty programs and databases (at least until Java
Database Connectivity (JDBC) becomes a standard, as well).
<H2><A NAME="WhatCGIProgramsCanDo"><FONT SIZE=5 COLOR=#FF0000>What
CGI Programs Can Do</FONT></A></H2>
<P>
CGI programs can do many things on many platforms. In fact, one
of the major features implemented in servers supporting CGI specification
as opposed to those using other proprietary server extensions
is that the programs can be written in any language, and on any
platform, as long as they conform to the specification. By comparison,
most server extensions are proprietary and specific to one server
software, one platform, or both. This means that it's not specifically
tied down to the pc, as is VBScript.
<P>
Table 1.1 is a comparison of functionality of normal HTML to HTML
enhanced with various CGI scripts.
<P>
<CENTER><B>Table 1.1. Tasks you can perform in CGI.</B></CENTER>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><I>Task</I></TD><TD WIDTH=103><CENTER><I>CGI+HTML</I></CENTER>
</TD><TD WIDTH=244><CENTER><I>HTML Alone</I></CENTER></TD></TR>
<TR><TD WIDTH=244>Handle forms</TD><TD WIDTH=103><CENTER>Yes</CENTER>
</TD><TD WIDTH=244><CENTER>No</CENTER></TD></TR>
<TR><TD WIDTH=244>Create almost anything non-static that needs to be on a Web page
</TD><TD WIDTH=103><CENTER>Yes</CENTER></TD><TD WIDTH=244><CENTER>No</CENTER>
</TD></TR>
<TR><TD WIDTH=244>Handle imagemaps</TD><TD WIDTH=103><CENTER>Yes</CENTER>
</TD><TD WIDTH=244>Yes (but only with client side imagemapping)
</TD></TR>
<TR><TD WIDTH=244>Add searching to a Web page or set of documents
</TD><TD WIDTH=103><CENTER>Yes</CENTER></TD><TD WIDTH=244><CENTER>No</CENTER>
</TD></TR>
<TR><TD WIDTH=244>Create forms</TD><TD WIDTH=103><CENTER>Yes</CENTER>
</TD><TD WIDTH=244><CENTER>Yes</CENTER></TD></TR>
<TR><TD WIDTH=244>Create platform-independent documents</TD><TD WIDTH=103><CENTER>Yes</CENTER>
</TD><TD WIDTH=244><CENTER>Yes</CENTER></TD></TR>
<TR><TD WIDTH=244>Create applications such as chat rooms, voting booths, or anything interactive
</TD><TD WIDTH=103><CENTER>Yes</CENTER></TD><TD WIDTH=244><CENTER>No</CENTER>
</TD></TR>
<TR><TD WIDTH=244>Allow pages to be generated on the fly, making it easier to update a group of pages
</TD><TD WIDTH=103><CENTER>Yes</CENTER></TD><TD WIDTH=244><CENTER>No</CENTER>
</TD></TR>
<TR><TD WIDTH=244>Create documents tailored specifically to each user
</TD><TD WIDTH=103><CENTER>Yes</CENTER></TD><TD WIDTH=244><CENTER>No</CENTER>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Not only can CGI do all these things, but basic CGI applications
are extremely easy in C, C++, Shell Scripting, and Perl.
<P>
To prove this, here is how to write a &quot;Hello World&quot;
CGI application in each of the languages mentioned in the preceding
sentence.
<P>
The &quot;Hello World&quot;/print all environment variables CGI
application in Perl is shown in Listing 1.1.
<HR>
<BLOCKQUOTE>
<B>Listing 1.1. How to print CGI environment variables in Perl.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/local/bin/perl<BR>
#Just returns an HTML Page with the words &quot;Hello World&quot;
<BR>
require &quot;cgi-lib.pl&quot;<BR>
<BR>
print &amp;PrintHeader;<BR>
print &quot;&lt;TITLE&gt;Hello World&lt;/TITLE&gt;&quot;<BR>
print &quot;Hello World&lt;P&gt;&quot;<BR>
print &amp;PrintEnv;<BR>
exit;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
In C, with CGI-HTML, the basic app that writes &quot;Hello World&quot;
and then prints out the CGI environment looks like Listing 1.2.
<HR>
<BLOCKQUOTE>
<B>Listing 1.2. How to print CGI environment variables in C.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#include &lt;stdio.h&gt;<BR>
#include &quot;html-lib.h&quot;<BR>
#include &quot;cgi-lib.h&quot;<BR>
<BR>
int main()<BR>
{<BR>
&nbsp;&nbsp;html_header();<BR>
&nbsp;&nbsp;html_begin(&quot;Test CGI&quot;);<BR>
&nbsp;&nbsp;h1(&quot;CGI Test Program&quot;);<BR>
&nbsp;&nbsp;printf(&quot;&lt;hr&gt;\n&quot;);<BR>
&nbsp;&nbsp;h2(&quot;CGI Environment Variables&quot;);<BR>
&nbsp;&nbsp;print_cgi_env();<BR>
&nbsp;&nbsp;html_end();<BR>
&nbsp;&nbsp;return 0;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The same program, written in C++ using the AHTML class library
appears in Listing 1.3.
<HR>
<BLOCKQUOTE>
<B>Listing 1.3. How to print CGI environment variables in C++.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#define _DEBUG_DUMP_<BR>
#include &quot;a_cgi.hpp&quot;<BR>
<BR>
main ()<BR>
{<BR>
&nbsp;&nbsp;ACGI acgiOut;<BR>
<BR>
&nbsp;&nbsp;acgiOut.mimeHTML();<BR>
&nbsp;&nbsp;acgiOut.cgiEnvironmentDump(0x1);&nbsp;&nbsp;&nbsp;&nbsp;
//a_Full dump!<BR>
<BR>
&nbsp;&nbsp;return 0x0;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Lastly, the shell script to print out environment variables appears
in Listing 1.4.
<HR>
<BLOCKQUOTE>
<B>Listing 1.4. How to print CGI environment variables with a
shell script.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/bin/sh<BR>
<BR>
echo Content-type: text/plain<BR>
echo<BR>
echo Hello World<BR>
echo SERVER_SOFTWARE = $SERVER_SOFTWARE<BR>
echo SERVER_NAME = $SERVER_NAME<BR>
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE<BR>
echo SERVER_PROTOCOL = $SERVER_PROTOCOL<BR>
echo SERVER_PORT = $SERVER_PORT<BR>
echo REQUEST_METHOD = $REQUEST_METHOD<BR>
echo HTTP_AccEPT = &quot;$HTTP_AccEPT&quot;<BR>
echo PATH_INFO = $PATH_INFO<BR>
echo PATH_TRANSLATED = $PATH_TRANSLATED<BR>
echo SCRIPT_NAME = $SCRIPT_NAME<BR>
echo QUERY_STRING = $QUERY_STRING<BR>
echo REMOTE_HOST = $REMOTE_HOST<BR>
echo REMOTE_ADDR = $REMOTE_ADDR<BR>
echo REMOTE_USER = $REMOTE_USER<BR>
echo CONTENT_TYPE = $CONTENT_TYPE<BR>
echo CONTENT_LENGTH = $CONTENT_LENGTH</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
If you look at the apps, you will notice that they are very simple,
and all are under 20 lines. This is another good thing about CGI.
You can have a lot of functionality without a lot of overhead
of handling routine CGI things. But there is a downfall. CGI is
limited by the fact that it can't maintain state automatically
because HTML is a stateless protocol, and CGI is used to generate
HTML (or images embedded in HTML). This means that it cannot be
used for certain tasks.
<H3><A NAME="WhatCGICantDo">What CGI Can't Do</A></H3>
<P>
As stated earlier, CGI's usefulness ends on the Web page. After
you leave the idea of generated documents that don't change after
they are generated (in terms of that specific instance of loading
the page), CGI is worthless. Real, live applications embedded
on Web pages are the realm of languages such as ActiveScript and
Java. These languages will be covered later in the book, specifically
in <A HREF="ch28.htm" >Chapter 28</A>, &quot;ActiveScript,&quot;
 and <U><FONT COLOR=#0000FF>Chapter 24</FONT></U>, &quot;Java
and JavaScript as Alternatives to CGI,&quot; respectively. Examples
of things you can't do in CGI are
<UL>
<LI><FONT COLOR=#000000>Create multi-player games</FONT>
<LI><FONT COLOR=#000000>Create stock tickers that are updated
across the page</FONT>
<LI><FONT COLOR=#000000>Create real applications embedded in Web
pages</FONT>
</UL>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A>
</H2>
<P>
As it stands right now, the usefulness of CGI as a standard to
program with varies from task to task and difficulty to difficulty.
The other major factor is time. CGI is a relatively old technology,
and as newer technologies, such as the Java programming language,
grow up, they will eventually make it obsolete. But for right
now, this has not happened yet, and even when it does, CGI will
still have its place.
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch2.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -