As web usage expanded, developers began wanting to use web browsers to do more than simply display static text and graphics. The web provided the perfect platform for accessing other applications on the web server from the browser interface. Just like browser developers did not want to create new image types for HTML, web server developers did not want to embed any and all possible server side functionality into the web server. For instance, many excellent database programs existed prior to the web and no one saw any need to add this functionality to web server programs. Thus CGI, the Common Gateway Interface, was born. CGI specifies a mechanism for a web page to invoke an arbitrary program on the web server. A common use of this is to use a form interface to input data into a database. Figure 20-2 looks at how this is done.
CGI programs can be written in any language. The Perl scripting language and C are probably the two most popular CGI programming languages. When the user enters the "submit" button on the form, the information contained in the form is passed, via HTTP, to the web server, along with the name of the CGI program to execute. The web server then starts up a new process to execute the specified program (a Perl script, in the example figure) and passes the form data to the program. The Perl script performs any necessary field validity checking or other preprocessing and then opens up a SQL connection to the database. The Perl program can then provide any desired response to the user by returning an HTML stream, through the web server, to the web browser.
CGI programming was a simple second generation mechanism for adding new functionality to web-based programs and for integrating back-end databases or other existing programs. This simplicity, however, comes with a performance penalty. CGI usually requires a new process to be spawned for each task to be performed. Especially when handling hundreds or thousands of tasks a minute, this becomes inefficient. Initially, these inefficiencies were handled by web server vendors who each developed their own proprietary APIs. Today, the Java Servlet API provides a common mechanism for expanding web server functionality via new Java applications, called Servlets.