If you want to get out of the level of asp enthusiasts, then you should learn to write components for asp. I searched on Google and found that there are only a few articles on Delphi writing asp components, so today I wrote this basic article on Delphi writing asp components, hoping that it will be helpful to novices. If you want to learn VB to write asp components, it is recommended to check out the article by Tornado Big Brother (http://blog.csdn.net/online/category/9939.aspx), which is not something I can compare with. hehe:)
Let's start, let's write an example of "hello world!" together. I'm using Delphi 7 here.
1. File->New->Other->ActiveX->ActiveX Library, and then save it as showdll.dPR
2. Again, file -> New -> Others -> ActiveX -> ActiveX Server Object, fill in CoClassName:showhello, others remain unchanged, click OK.
3. Now start writing the program and add a method first. Select Ishowhello->right-click->New->Method, and fill in the method name: sayworld.
4. Now start writing the program, save Unit1 as show.pas, and then add the code of the method sayworld method
The full code of show.pas is as follows:
unit show;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, AspTlb, showdll_TLB, StdVcl;
type
Tshowhello = class(TASPObject, Ishowhello)
protected
procedure OnEndPage; safecall;
procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
procedure saysworld; safecall; //sayworld method
end;
Implementation
uses ComServ;
procedure Tshowhello.OnEndPage;
Begin
inherited OnEndPage;
end;
procedure Tshowhello.OnStartPage(const AScriptingContext: IUnknown);
Begin
inherited OnStartPage(AScriptingContext);
end;
procedure Tshowhello.sayworld(); //Define the sayworld method
Begin
response.Write('Hello world'); //The syntax inside is written in the same way as asp, and it is encapsulated here.
end;
Initialization
TAutoObjectFactory.Create(ComServer, Tshowhello, Class_showhello,
ciMultiInstance, tmApartment);
end.
4. Click Run, compile it into dll, and automatically register it. At this time, you will prompt:
Let you put it on the web server to run. Now write an asp file and call it. Note that Delphi has generated an asp file, so we can change the calling method.
The modified showhello.asp code is as follows:
<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("showdll.showhello")
DelphiASPObj.sayworld
%>
<HR>
</BODY>
</HTML>
Run it under iis's site to see the effect:
5. other:
The component written by Delphi, after registering with win2000 component service, you can see the interface method of the component
6. There are also parameters that pass between the asp page and the component, which is actually to pass parameters to the called method (function). Note that the definition in Delphi must be consistent with the data type of vbs. Please practice these more. This is mainly to learn how to encapsulate the core code of Asp and play a role in attracting jade.