ASP服务器组件的编程

asp服务器组件

asp(active server page)是当今开发交互式web页面、web数据库应用最强大的技术。在其中可以混用html、dhtml、
activex、vbscript或javascript。当这些技术都无法奏效时(例如进行高密度的数学运算、封装特定的数据库处理逻辑
等),可以使用服务器组件(server sidecomponent)进一步扩展asp的能力。
server sidecomponent实际上是运行在服务器上的一个dll,它可以完成常规dll所能胜任的任何任务。不同之处是:它由
asp页面调用、并以web页面为交互对象,读入用户的输入(web页面上各输入域的值),处理后返回结果到web页面。这些
交互当然都要通过web服务器作为中介。可以用vb、vfp、vc++、c++builder、delphi等任意支持com技术的语言编写。由于
它可以利用服务器的任何资源,其功能仅受限于你的想象力。
目前支持asp的web服务器有iis(internet information server,winnt server4.0自带)和pws(personel web server,
用于win95环境)。并要求安装visualinterdev中的server components:frontpage server extensions、 active
serverpages和client components:visual interdevclient。可以把这些都安装在同一台机器上,这样可以在单机上方便
地编程、调试。
下面用vb5.0开发一个server side component(一个activexdll),以实现web页面上的随机图形显示,相信它会为你的站
点增色不少。

2. web页面上的随机图形显示

一个漂亮的图形可以使web页面更具吸引力,使人流连忘返。但一旦我们的web页面设 计完成,这个图形也就确定下来。换
言之,除非我们重新修改html代码,则每次打开这个页面,看到的都是同样一个图形。那么能否让用户在每次进入我们的
站点时,都能看到不同的画面呢?例如:每次这个web页面被访问时,从一个包含若干图形文件的文件夹中随机选取一个,
在该页面上显示,使访问该页面的用户每次都会得到不同的视觉享受。
这个要求用html、dhtml或vbscript语言无法做到,这里我们用一个asp服务器组件实现之。

3.用vb5.0建立activex dll

首先在vb5.0中新建一个project ,类型为activex dll :设定属性如下:
project name:randshowfile,
classmodule name:randimage
其中类randimage的代码如下:
option explicit
private mvarfilepath as string ’local copy
public property let filepath(byval vdata as string)
’设置文件路径
if right(vdata, 1) = "/" or right(vdata, 1) = "/" then
mvarfilepath = vdata
else
if instr(vdata, "/") <> 0 then
mvarfilepath = vdata & "/"
else
mvarfilepath = vdata & "/"
end if
end if
end property

public property get filepath() as string
’取得文件路径
filepath = mvarfilepath
end property

private sub class_initialize()
mvarfilepath = ""
end sub

public function show(optional byval extension as string) as string
’从指定文件路径中随机选取并返回一个文件名
dim mypath as string
dim myname as string
dim list() as string
dim filecount as integer
dim n as integer
on error goto badnews
if len(mvarfilepath) <= 1 then
show = "nofilepathspecified "
erase list
exit function
else
if ismissing(extension) then
extension = "*.*" ’如果扩展名没有指定,则默认为*.*
end if
mypath = mvarfilepath & trim(extension) ’ set the path.
myname = dir(mypath, vbnormal) ’ retrieve the first entry.
end if
filecount = 0
redim list(10)
do while myname <> ""
list(filecount) = myname
filecount = filecount + 1
if filecount >= ubound(list) then
n = ubound(list) + 10
redim preserve list(n)
end if
myname = dir()
loop
if filecount >= 1 then
randomize ’ 初始化rand()函数,否则每次将产生相同的数字
n = int(filecount * rnd()) ’ 产生在1 和list1.listcount 之间的随机数.
show = list(n)
erase list
exit function
else
badnews:
show = "nofilefound"
erase list
end if
end function
在编译之前,注意要在此project中加入一个module并在其中加入代码
sub main()
end sub
然后在菜单project | randshowfile projectise?引出的对话框中,设startup
object为sub main。最后在菜单file中,选make randimage.dll。到此,我们的ssc
就开发完成,并且它已自动注册在机器上。

4.在asp页面中使用服务器组件

下面将建立一个asp页面以测试我们的server side component。
启动visual interdev,开始一个新的工程:new projects,然后选取web project wizard,在project name中输入
testrandimage,点击ok后,visual interdev产生一些辅助文件,为新的工程做好准备,然后自动打开该工程。为了方便
测试,拷贝几个图形文件到images文件夹中,文件类型可以是浏览器支持的任意图形文件,如bmp、tif、gif等 。
在该工程中建立asp页面,点击菜单file | new ,在new 对话框中选files | active server page ,并指定其名字:
randimage.asp。visual interdev将会为我们产生一个空的框架,在其中用手工加入代码。完成后的代码如下

<%@ language="vbscript" %>
<html><head>
</head>
<body>
<h5>测试randimage 组件,随机显示一个图形文件<h5>图形文件路径:
<%=server.mappath("images")%><br>
<%set
ox=server.createobject("randshowfile.randimage")’实例化组件ox.filepath=serve
r.mappath("images")
%>
<img src="<%=ox.filepath&ox.show%>">
<%set ox=nothing ’使用后释放组件%>
</body>
</html>
由于web页面使用的路径(url)都是虚拟路径(virtual directory),必须使用server.mappath()将其转换到物理路径
(physical directory)。例如,此处的图形文件夹images的虚拟路径是://servername/testrand image/images(其中
servername是你的web服务器的名字),其对应的物理路径是c:/inetpub/wwwroot/testrandimage/images 。如果不把
images映射到物理路径则组件找不到该文件夹,无法正常工作。 代码完成后测试之,注意到在每次打开或刷新该页面时,
会有一个不同的图形显示在上面。

5.结束语

使用ssc可以大大丰富web应用的功能、提高编程效率;完成html或vbscript等不易完成的任务;封装特定的商业逻辑等。
server side component(以及activex)等组件的编程也发展成为一项有利可图的事业。在internet上可以找到很多有用
的组件(免费的或不免费的),有兴趣者可到www.15seconds.com、www.activeserverpages.com、www.serverobjects.com
等站点上查看。如果你有一
个新颖有用的组件,也可以发表在这些站点上,说不定你可以因此得到一笔可观的收入呢。(重庆出版社电脑中心 陈刚) 

from专业的网络技术,黑客教程,精品小说交流平台

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值