<HTML> <HEAD> <LINK REL=STYLESHEET TYPE="text/css" href="/home-styles.css"> <TITLE> Delphi ASP Object Demo</TITLE> </HEAD> <BODY> <CENTER><H2>Vendors Table from DBEMOS</H2></CENTER> <HR> <% Set DelphiASPObj = Server.CreateObject("DelphiASP.Vendor") DelphiASPObj.GetVendors %> <HR> </BODY> </HTML>
procedure TVendor.GetVendors; var sSQL : string; begin sSQL := Request.Form['txtSQL']; Response.Write(DM.GetVendorInfoPage(sSQL)); end;
<HTML> <HEAD> <TITLE> Delphi ASP Object Demo</TITLE> </HEAD> <BODY> <CENTER><H2>Vendors Table from DBEMOS</H2></CENTER> <HR> <% Set DelphiASPObj = Server.CreateObject("DelphiASP.Vendor") DelphiASPObj.GetVendors %> <HR> </BODY> </HTML>
Set DelphiASPObject = Server.CreateObject("DelphiASP.Vendor")
DelphiASPObject.GetVendors
<HTML> <BODY> <TITLE> Testing Delphi ASP </TITLE> <CENTER> <H3> You should see the results of your Delphi Active Server method below </H3> </CENTER> <HR> <% Set DelphiASPObj = Server.CreateObject("Project1.Vendor") DelphiASPObj.{Insert Method name here} %> <HR> </BODY> </HTML>
<% Set DelphiASPObj = Server.CreateObject("Project1.Vendor")
unit uVendor_Impl; interface uses ComObj, ActiveX, AspTlb, DelphiASP_TLB, StdVcl; type TVendor = class(TASPObject, IVendor) protected procedure OnEndPage; safecall; procedure OnStartPage(const AScriptingContext: IUnknown); safecall; end; implementation uses ComServ; procedure TVendor.OnEndPage; begin inherited OnEndPage; end; procedure TVendor.OnStartPage(const AScriptingContext: IUnknown); begin inherited OnStartPage(AScriptingContext); end; initialization TAutoObjectFactory.Create(ComServer, TVendor, Class_Vendor, ciMultiInstance, tmApartment); end.
procedure Write(varText: OleVariant); safecall;
Response.Content := 'Hello World'; In our ASP object it would be Response.Write('Hello World'); If on the other hand we had this in our ISAPI: Response.Content := 'Hello'; Response.Content := Response.Content 'World'; In our ASP object it would be Response.Write('Hello'); Response.Write('World');
Response.Write(DM1.GetVendorInfoPage);
unit uVendor_Impl; interface uses ComObj, ActiveX, AspTlb, DelphiASP_TLB, StdVcl, uDM; type TVendor = class(TASPObject, IVendor) protected procedure OnEndPage; safecall; procedure OnStartPage(const AScriptingContext: IUnknown); safecall; procedure GetVendors; safecall; end; implementation uses ComServ; procedure TVendor.OnEndPage; begin inherited OnEndPage; end; procedure TVendor.OnStartPage(const AScriptingContext: IUnknown); begin inherited OnStartPage(AScriptingContext); end; procedure TVendor.GetVendors; begin Response.Write(DM.GetVendorInfoPage); end; initialization TAutoObjectFactory.Create(ComServer, TVendor, Class_Vendor, ciMultiInstance, tmApartment); end.
function GetVendorInfoPage(sSQL : string) : string;
unit uDM; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TDM1 = class (TDataModule) Query1: TQuery; Session1: TSession; DataSetTableProducer1: TDataSetTableProducer; private { Private declarations } public function GetVendorInfoPage : string; { Public declarations } end; var DM1: TDM1; implementation {$R *.DFM} { TDM1 } function TDM1.GetVendorInfoPage: string; begin end; end.
Result := DataSetTableProducer1.Content;
unit uVendor_Impl; interface uses ComObj, ActiveX, AspTlb, DelphiASP_TLB, StdVcl, uDM; type TVendor = class(TASPObject, IVendor) protected FDM : TDM1; procedure OnEndPage; safecall; procedure OnStartPage(const AScriptingContext: IUnknown); safecall; procedure GetVendors; safecall; property DM : TDM1 read FDM; end; var DM : TDM1; implementation uses ComServ; procedure TVendor.OnEndPage; begin FDM.Free; FDM := nil; inherited OnEndPage; end; procedure TVendor.OnStartPage(const AScriptingContext: IUnknown); begin inherited OnStartPage(AScriptingContext); FDM := TDM1.Create(nil); end; procedure TVendor.GetVendors; begin Response.Write(DM.GetVendorInfoPage); end; initialization TAutoObjectFactory.Create(ComServer, TVendor, Class_Vendor, ciMultiInstance, tmApartment); end.
<HTML> <BODY> <TITLE> Testing Delphi ASP </TITLE> <CENTER> <H3> You should see the results of your Delphi Active Server method below </H3> </CENTER> <HR> <% Set DelphiASPObj = Server.CreateObject("Project1.Vendor") DelphiASPObj.GetVendors %> <HR> </BODY> </HTML>
http://mydomain/vendor.asp
Using the Response Editor, lets set the Border attribute of the HTML Table to 1. Using the OnFormatCell event we can manipulate the HTML table even further. Here is what the OnFormatCell event of the TDataSetTableProducer should look like to produce an output similar to the demo you saw at the beginning of this article.
procedure TDM1.DataSetTableProducer1FormatCell(Sender: TObject; CellRow, CellColumn: Integer; var BgColor: THTMLBgColor; var Align: THTMLAlign; var VAlign: THTMLVAlign; var CustomAttrs, CellData: String); begin { Color the First Column } if CellColumn = 0 then BgColor := 'Teal'; { Color the Second Column } if CellColumn = 1 then BgColor := 'Olive'; { Color the Second Row - excluding the Field Name Row } if CellRow = 2 then BgColor := 'Red'; { Color all instances of the Field - Preferred where the Value = False } if CellColumn = 10 then begin if CellData = 'F' then BgColor := 'Yellow'; end; end;
So we're going to need:
sSQL := Request.ContentFields.Values['txtSQL'];
In ASP, the Request object is a bit different. It has a property called Form. The way you would access the fields of a Form in ASP is:
sSQL := Request.Form['txtSQL'];
Currently, our GetVendorInfoPage method does not expect any parameters, so we'll have to modify it such that it expects to see a string parameter and then use this parameter to assign it to our Query object and get a result set.
The GetVendors method of our ASP object will now look like this:
function TDM1.GetVendorInfoPage(sSQL : string) : string; begin with Query1 do begin Close; SQL.Clear; SQL.Add(sSQL); Open; end; Result := DataSetTableProducer1.Content; end;
This concludes the changes we need to make to our ASP object such that it receives the SQL statement via an HTML form. We still need the HTML form however. The HTML form's source code looks like this:
<HTML> <HEAD> <TITLE>Delphi ASP Demo</TITLE> </HEAD> <BODY> <FORM ACTION="http://www.matlus.com/Vendor.asp" METHOD="POST"> The <B>ASP Object</B> is connected to the VENDORS Table in DBDEMOS. Feel Free to Change the SQL Statement below to a Valid SQL Statement for this Table. <P> <TEXTAREA NAME="txtSQL" COLS=40 ROWS=5>SELECT * FROM VENDORS</TEXTAREA> <BR> <INPUT TYPE="SUBMIT"> </FORM> </BODY> </HTML> </PRE>
To test our work, we need to load the Vendor.htm in our browser, set the SQL statement if required (you should test this) and clicking on the SUBMIT button on the Form. Figure 5 shows what the output will look like with an SQL statement of: SELECT VendorNo, VendorName, City, State, Zip, Preferred FROM VENDORS
Figure 5 Showing the output generated by the ASP object
The flaw in our design here is that there is no column 10 and so even though the Preferred field is amongst the one we want displayed, it is not colored yellow where the value = False. We'll leave that as an exercise for those of you who would want to see this work.
While no benchmarks are really going to give you the complete story, these benchmarks can tell you something. If WebClasses are slower, they aren't really that much slower (30%). Most of the performance loss is simply due to creating and destroying WebClass instances, which is not a sufficient reason to avoid using WebClasses. Moreover, if you're not willing to give up a little performance to get some extra productivity, you shouldn't be using Visual Basic or the ASP framework-you should be writing custom ISAPI extension DLLs with C .