Databases - Delphi CGI Web Server

The purpose of a Web Server is to generate a web page (html) that is displayed in a regular web browser.

A CGI Web Server is an *.exe file - ISAPI and NSAPI Web Servers are *.dll files.

*.dll Web Servers are better (faster and more efficient) for high volume sites, but they are very difficult to debug because the main Web Server (IIS, Netscape, Apache, and the like) must be shut down each time a new *.dll is compiled. As a result, it is always easier to create a CGI server first and convert it to an *.dll latter. (Only the project (*.dpr) file is different.)

See C:\Program Files\BORLAND\Delphi5\Demos\Webserv for a demo where the CGI (*.exe) and ISAPI (*.dll) applications use a common module (*.pas file).

For an overview, see Creating Internet server applications in the Delphi help. (In the help contents, it is under Writing Distributed Applications. You can also launch it via Start / Programs / Delphi / Help / Developing Distributed Applications, but then many of the links won't work.)

Creating the Application | Producing html | TDataSetTableProducer | Reading Parameters

Running your program under IIS | Version Issues | Testing

Creating the Application

From the menu, select Then, from the wizard, you select CGI stand-alone executable.

Web servers do not have a "form" as such (it actually runs as a console application), instead they produce html which is sent back to a browser.

This example will access the Interbase/Firebird database employee.gdb (it ships with Delphi).

If you edit some parameter and no data is displayed, check IBDatabase1.Connected, IBTransaction1.Active, and IBTable1.Active, in that order. (Actually, just setting IBTable1.Active to True will automatically set the others.)

To connect to the Interbase example database distributed with Delphi, set IBDatabase1.DatabaseName to

This is only one of many ways to produce a Web Server. For an advanced application, you will probably want many WebActionItem's - each one associated with a separate function - and a default WebActionItem to handle 404 (page not found) type errors.

Producing html

Web Servers automatically create a TWebResponse object. When data is requested from a page (server), a WebActionItem is called (your application may have several of them) with the TWebResponse object passed to the OnAction event handler as a parameter named Response. html can be returned by setting Response.Content to an html string. If WebActionItem.Producer is set, whatever html the producer generates will also be sent.

This will manually return the html from a "Producer"

These are the main producers


The TDataSetTableProducer formats a table into an html table.

However, several properties have problems - they don't work at all UNLESS you replace the default fields with one you have "manually" entered.

At design time, it is possible to set properties for the individual columns and their headers. The changes take effect immediately in the provided dialog box, but do not appear in the final output.

Formating Individual Cells

This TDataSetTableProducer code (attached to the OnFormatCell event) converts a field stored as Single to a Date/Time. Notice that the Row must be greater than zero to avoid the column headers.

Reading Parameters

Web Servers automatically create a TWebRequest object that is passed to a WebActionItem OnAction event handler as a parameter named Request.

Various http headers can be read as TWebRequest properties, many more can be read via

Query parameters should be read via the QueryFields name/value pair String List

Running your program under IIS

To run your code under IIS, you must place it in a directory that has permission to execute programs.

Your files are stored as

and accessed as The Apache configuration is slightly different.

Version Issues

HTTPProd is used in Delphi 6, but not present in Delphi 5.


The Delphi help says that CGI files are more difficult to test than *.dll's - I don't agree at all.

There are several ways to test your CGI programs

Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Databases / Delphi / WebServers / WebServer_CGI.html