Thursday, February 1, 2007

WK3 - Let me talk about "Pros and Cons of CGI"

There are advantages and disadvantages to writing your Web applications as CGI programs.
I’ll talk about the good stuff first – the advantages of CGI programming.

The main advantage of CGI programming is that it’s ultimate cross-platform technology. It works on Web servers running both Windows and UNIX, and with almost every Web server. So when you write CGI programs, you can be fairly certain that they’ll be portable to whatever environment you’ll want to run them in. The second major advantage of CGI is that it’s language of your choice. There‘s no need to learn a new programming language just to write CGI programs. If you choose a cross-platform language, like Perl, Python, it’s trivial to port your programs from UNIX to Windows, or vice visa.

Another advantage of CGI is that it’s a very simple interface. It’s not necessary to have any special libraries to create a CGI program, or write your programs to use a particular API. Instead, CGI programs rely on the standard UNIX concepts of standard input, standard output and environment variables to communicate with the web server.

Now let’s take a look at the disadvantage of CGI. The single greatest disadvantage of CGI program comes into play when write your programs in a scripting language. Every time a CGI program is requested, the interpreter for the scripting language has to be started, the script has to be evaluated, and then the script has to be executed. The fact that you have to run the Python interpreter every time a Python CGI script is requested is very inefficient. Whether this is a problem depends on how powerful your Web server is, how many requests there are for your CGI scripts, and how long it takes the CGI programs load. Generally speaking, the performance issue does not become a problem unless you run a very high traffic Web site, or you have an antiquated Web server.

People who write their CGI program in a compiled language like C don’t have to deal with this problem, because there’s no extra overhead like that generated by an interpreter. In fact, it was once common to use small, fast-executing CGI programs as a gateway between the Web server and the application server process. That allowed the application server to work with Web servers that they can’t communicate with through a native interface.

The other main complaint about CGI program is that they don’t make things as easy on Web programmers as some other newer Web application platforms. When you write a CGI program, in addition to all of the program logic that creates the functionality you want, you also have to write code to generate the HTML code for the page. Most of today’s popular application servers allow you to embed program logic within a standard HTML page, which can save some work when you write the programs. These application servers are also easier to learn for people who know HTML but don’t know how to program. It is, however, harder to write a structured, well organized programs when you use this type of technology, so the choice really is really one of preference. One isn’t absolutely better than another.

1 comment:

Anonymous said...

Word for word (pretty much) from the Sams Teach Yourself book. You must be the author.