论坛: 网站建设 标题: 用delphi开发asp服务器端组件(HelloWorld!篇) 复制本贴地址    
作者: newekin [newekin]       登录
作者:绝对零度 (newekin)
email:webmaster@rohu.com



Delphi一直是windows平台上的一个非常流行的开发工具,快速、高效以及其在数据库和网络上的开发优

势,一直让无数专业开发人员为之倾倒。
asp是MicroSoft推出的一项动态web技术,它简单、实用,在动态web技术上占有一席之地,就算Microsof

t推出了号称asp下一版本的asp.net ,但是暂时还难以替代asp在动态web技术上的地位。
asp虽然很好,但是它也有不足之处,当今的很多系统都是采用b/s架构,客户端用浏览器,服务器端有we

b服务器,应用程序服务器,以及数据库服务器等,由于支持asp的都是脚本语言(像vbscript,javascri

pt,perlscript等),如果一个大型的系统(web服务器,应用程序服务器、还有数据库服务器),采用

单纯的asp去实现,是很难的,就算实现了,效率也很低。采用asp组件技术就可以解决这个问题,asp组

件可以从根本上扩展asp的功能。asp组件一般是采用vb,delphi,vc,java等编写。下面就给出一个用de

lphi开发asp组件的例子
打开delphi,点击菜单 File->New->Other  选择Activex->Activex Library  保存项目为

HelloWorld.dpr 
继续选择菜单File->New->Other 选择 Activex->Active Server Object  在CoClass Name 中添入

hello 
保存库文件为  helloASP.pas  保存asp文件为hello.asp
选择 Ihello节点  右击 New->Method  新建一个方法  将方法名(name)改为 test  在helloasp.pas中的

test的实现部分填加下列代码
response.Write('Hello World!');
在hello.asp中替换{Insert Method name here}为test 方法
然后保存全部工程文件,选择 Run->Register Activex Server  注册组件

将hello.asp文件复制到你的iis中执行asp的文件夹,执行即可。

/*------------------------
下面是全部 HelloASP.pas 代码
----------------------------*/
unit helloASP;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, AspTlb, HelloWorld_TLB, StdVcl;

type
  Thello = class(TASPObject, Ihello)
  protected
    procedure OnEndPage; safecall;
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    procedure test; safecall;
  end;

implementation

uses ComServ;

procedure Thello.OnEndPage;
begin
  inherited OnEndPage;
end;

procedure Thello.OnStartPage(const AScriptingContext: IUnknown);
begin
  inherited OnStartPage(AScriptingContext);
end;

procedure Thello.test;
begin
  response.Write('Hello World!');
end;

initialization
  TAutoObjectFactory.Create(ComServer, Thello, Class_hello,
    ciMultiInstance, tmApartment);
end.

/*------------------------------------------------------
下面是hello.asp代码
-------------------------------*/
<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("HelloWorld.hello")
  DelphiASPObj.test
%>
<HR>
</BODY>
</HTML>

/*------------------------------------
helloworld.dpr 文件代码
-------------------------------------*/
library HelloWorld;

{%File 'hello.asp'}

uses
  ComServ,
  HelloWorld_TLB in 'HelloWorld_TLB.pas',
  helloASP in 'helloASP.pas' {hello: CoClass};

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
end.

/*-------------------------------------------
以上代码在win2000+iis5+ie5.0  环境下测试通过
---------------------------------------------*/

地主 发表时间: 04-02-25 10:42

论坛: 网站建设

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号