1.0. Functions of CGI program:
First, what is Common Gateway Interface (CGI)? CGI is a standard used to define communication methods between WEB servers and external programs, so that external programs can generate HTML, images or other content, and the server handles HTML, images or other content that is not generated by external programs. The processing method is the same. Therefore, CGI programs enable you to generate not only static content, but also dynamic content. The reason for using CGI is that it is a well-defined and widely supported standard. Although java, ActiveX, etc. can complete the functions of CGI programs, not all browsers support them. On the contrary, all browsers support CGI, such as: Lynx, IE, Netscape, etc.
However, like other technologies, CGI has its limitations. In this section, I will describe the functions, advantages and disadvantages of CGI programs.
1.1. CGI function
There are many tasks for which CGI is the best and only option, and these tasks can be divided into three categories: beginner tasks, intermediate tasks and advanced tasks. Elementary tasks are tasks that require little programming, such as:
text counter
Program to generate simple HTML
Programs of less than 50 lines written in Perl, Shell Script, C, or C++
There are at least three advantages to using CGI to write such tasks: First, CGI runs the fastest, while Java is too expensive; second, the CGI standard is the most compatible with current browsers, which has been mentioned before; third, CGI resources are abundant, and you can find thousands of CGI codes on the Internet.
Intermediate tasks include image mapping and other slightly more complex programming tasks. At the intermediate level, there is little difference in the ease of writing programs in CGI and Java.
Such tasks include:
image mapping
CGI script to generate full page HTML
animation
Characteristically, for advanced tasks, CGI is much simpler than programming in Java. Such tasks include:
Backend database operations
search engine
Multiple dynamic pages
Among them, back-end database operations (applications that access databases) reflect the superiority of CGI. CGI has a certain history, in which many useful functions have been done by others in the CGI library; at the same time, many large companies have provided source code made with CGI. Java is a programming language, and CGI is the functional specification of the gateway program. If there are major changes in Java, you have to rewrite the entire program; and if there are major changes in CGI, you only need to upgrade the CGI library program.
However, CGI has limitations in some areas.
1.2. Limitations of CGI
In terms of image mapping and animation, CGI programs are not as easy to develop as Java programs. Now that Java is becoming more and more popular, CGI programs are becoming more and more suitable for writing short and complex programs and database applications.
1.3. CGI program functions
The biggest feature of a CGI program is that it can be compiled in any language and run on any platform, as long as it complies with CGI specifications. The following table is a comparison:
TaskCGI+HTML HTML
Process the formYes No
Create non-static content on WEB pagesYes No
Process image image files Yes Yes
Search for Yes No in WEB pages and documents
Create formYes Yes
Create platform-independent documents Yes Yes
Create interactive applications such as chat rooms Yes No
Page dynamic generation Yes No
Cut page documents according to user needs Yes No
Next, we end this lecture with a series of "Hello World" programs:
Perl:
Require "cgi-lib.pl";
PRint&PrintHeader;
print " ";
print "Hello World";
print &PrintEnv;
exit;
This file is stored in the C:HTTPDCGI-BIN directory and is named 2_1.pl. Call in the browser using the address http://localhost/cgi-bin/2_1.pl. Note that OmniHTTPD should be running at this time. If everything goes well, you can see "Hello World" and environment variables in your browser.
C: #include
#include "html-lib.h"
#include "cgi-lib.h"
int main()
{
html_header();
html_begin("Test CGI");
h1("CGI Program");
printf(" ");
h2("CGI Environment Variables");
print_cgi_env();
html_end();
return 0; }
After compilation, copy the executable file to C:HTTPDCGI-BIN, rename it to 2_1.cgi, and call it with the address http://localhost/cgi-bin/2_1.cgi.