Introduction
This "simplest" CGI program uses the powerful web module and the
various CGI support components to provide a CGI app that either presents a message
or a display of the content of a simple database table (ID, NAME, ADDRESS fields).
You'll need to create the table and set up the alias.
Be aware that not all web servers work with CGI apps in the same way. Try to
test with at least two when you have a problem, to make sure the problem is
not a web server problem.
The following are the actions supported by this CGI...

The WebModule
Create a web module with File | New | Web Server Application and paste this
in...
object Web: TWeb
OldCreateOrder = False
Actions = <
item
Enabled = True
MethodType = mtGet
Name = 'Test'
PathInfo = '/Test'
OnAction = WebTestAction
end
item
Enabled = True
MethodType = mtGet
Name = 'Accounts'
PathInfo = '/Accounts'
OnAction = WebAccountsAction
end
item
Enabled = True
Default = True
Name = 'Default'
PathInfo = '/Default'
OnAction = WebDefaultAction
end>
Left = 236
Top = 561
Height = 156
Width = 449
object ResponsePage: TPageProducer
HTMLDoc.Strings = (
'<html>'
'<head>'
' <title>TestCGI</title>'
'</head>'
'<body>'
''
'<h1>'
'This Is A Test</h1>'
'This is a test HTML returned by TestBasicCGI'
'<br>You made a request'
'<p>Did it work?'
'</body>'
'</html>')
OnHTMLTag = ResponsePageHTMLTag
Left = 88
Top = 32
end
object ResponseTable: TDataSetTableProducer
Caption = 'Accounts'
Columns = <
item
FieldName = 'ID'
end
item
FieldName = 'NAME'
end
item
FieldName = 'ADDRESS'
end>
DataSet = AccountTable
Left = 224
Top = 32
end
object AccountTable: TTable
Active = True
DatabaseName = 'TestCGI'
TableName = 'Account.DBF'
Left = 328
Top = 32
end
end
The Web Producer Code And Event Handlers
Note that the event handlers may become detached when you paste the form into
the Web Module. Thus, you may have to reattach them by
a) Adding the function headers to the class definition and
b) Picking the appropriate handlers from the drop downs as suggested by the
handler names below
In any event, modify the WebModule code to essentially correspond to the following...
//---------------------------------------------------------------------------
#include "WebUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TWeb *Web;
//---------------------------------------------------------------------------
__fastcall TWeb::TWeb(TComponent* Owner)
: TWebModule(Owner)
{
WhichRequest = 0;
}
//---------------------------------------------------------------------------
__fastcall TWeb::~TWeb(void)
{
}
//---------------------------------------------------------------------------
void __fastcall TWeb::WebDefaultAction(TObject *Sender,
TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
Response->StatusCode = 250;
Response->ReasonString = "Default - " + Request->Accept
+ ", " + Request->Method;
Response->Content = ResponsePage->Content();;
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TWeb::ResponsePageHTMLTag(TObject *Sender, TTag Tag,
const AnsiString TagString, TStrings *TagParams,
AnsiString &ReplaceText)
{
if (TagString == "Request") ReplaceText = String(WhichRequest++);
}
//---------------------------------------------------------------------------
void __fastcall TWeb::WebTestAction(TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled)
{
Response->StatusCode = 260;
Response->ReasonString = "Test - " + Request->Accept
+ ", " + Request->Method;
Response->Content = ResponsePage->Content();
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TWeb::WebAccountsAction(TObject *Sender,
TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
Response->StatusCode = 270;
Response->ReasonString = "Accounts - " + Request->Accept
+ ", " + Request->Method;
Response->Content = ResponseTable->Content();
Handled = true;
}
//---------------------------------------------------------------------------
Conclusion
My experience with BCB CGI is that it is very powerful and very easy to use
without almost any knowledge of CGI. The above code can be used in ISAPI, NSAPI
or WinCGI without any changes.
The biggest difficulty is in testing. This requires a reliable web server which
really supports all CGI features. I tried OmniHTTPd and TinyWeb and both had
problems. Reportedly, the Microsoft Personal Web Server is free (for download
or comes with the OS) and is a better platform.
Also, if you seem to be getting nothing back in your browser it can be a good
idea to look at the document source. You sometimes are getting something back
but it doesn't show.
Good luck!
|
|