asp服務(wù)器組件
asp(active server page)是當(dāng)今開發(fā)交互式web頁面、web數(shù)據(jù)庫應(yīng)用最強(qiáng)大的技術(shù)。在其中可以混用html、dhtml、
activex、vbscript或javascript。當(dāng)這些技術(shù)都無法奏效時(shí)(例如進(jìn)行高密度的數(shù)學(xué)運(yùn)算、封裝特定的數(shù)據(jù)庫處理邏輯
等),可以使用服務(wù)器組件(server sidecomponent)進(jìn)一步擴(kuò)展asp的能力。
server sidecomponent實(shí)際上是運(yùn)行在服務(wù)器上的一個(gè)dll,它可以完成常規(guī)dll所能勝任的任何任務(wù)。不同之處是:它由
asp頁面調(diào)用、并以web頁面為交互對象,讀入用戶的輸入(web頁面上各輸入域的值),處理后返回結(jié)果到web頁面。這些
交互當(dāng)然都要通過web服務(wù)器作為中介。可以用vb、vfp、vc++、c++builder、delphi等任意支持com技術(shù)的語言編寫。由于
它可以利用服務(wù)器的任何資源,其功能僅受限于你的想象力。
目前支持asp的web服務(wù)器有iis(internet information server,winnt server4.0自帶)和pws(personel web server,
用于win95環(huán)境)。并要求安裝visualinterdev中的server components:frontpage server extensions、 active
serverpages和client components:visual interdevclient。可以把這些都安裝在同一臺(tái)機(jī)器上,這樣可以在單機(jī)上方便
地編程、調(diào)試。
下面用vb5.0開發(fā)一個(gè)server side component(一個(gè)activexdll),以實(shí)現(xiàn)web頁面上的隨機(jī)圖形顯示,相信它會(huì)為你的站
點(diǎn)增色不少。
2. web頁面上的隨機(jī)圖形顯示
一個(gè)漂亮的圖形可以使web頁面更具吸引力,使人流連忘返。但一旦我們的web頁面設(shè) 計(jì)完成,這個(gè)圖形也就確定下來。換
言之,除非我們重新修改html代碼,則每次打開這個(gè)頁面,看到的都是同樣一個(gè)圖形。那么能否讓用戶在每次進(jìn)入我們的
站點(diǎn)時(shí),都能看到不同的畫面呢?例如:每次這個(gè)web頁面被訪問時(shí),從一個(gè)包含若干圖形文件的文件夾中隨機(jī)選取一個(gè),
在該頁面上顯示,使訪問該頁面的用戶每次都會(huì)得到不同的視覺享受。
這個(gè)要求用html、dhtml或vbscript語言無法做到,這里我們用一個(gè)asp服務(wù)器組件實(shí)現(xiàn)之。
3.用vb5.0建立activex dll
首先在vb5.0中新建一個(gè)project ,類型為activex dll :設(shè)定屬性如下:
project name:randshowfile,
classmodule name:randimage
其中類randimage的代碼如下:
option explicit
private mvarfilepath as string ’local copy
public property let filepath(byval vdata as string)
’設(shè)置文件路徑
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
’從指定文件路徑中隨機(jī)選取并返回一個(gè)文件名
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 = "*.*" ’如果擴(kuò)展名沒有指定,則默認(rèn)為*.*
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()函數(shù),否則每次將產(chǎn)生相同的數(shù)字
n = int(filecount * rnd()) ’ 產(chǎn)生在1 和list1.listcount 之間的隨機(jī)數(shù).
show = list(n)
erase list
exit function
else
badnews:
show = "nofilefound"
erase list
end if
end function
在編譯之前,注意要在此project中加入一個(gè)module并在其中加入代碼
sub main()
end sub
然后在菜單project | randshowfile projectise?引出的對話框中,設(shè)startup
object為sub main。最后在菜單file中,選make randimage.dll。到此,我們的ssc
就開發(fā)完成,并且它已自動(dòng)注冊在機(jī)器上。
4.在asp頁面中使用服務(wù)器組件
下面將建立一個(gè)asp頁面以測試我們的server side component。
啟動(dòng)visual interdev,開始一個(gè)新的工程:new projects,然后選取web project wizard,在project name中輸入
testrandimage,點(diǎn)擊ok后,visual interdev產(chǎn)生一些輔助文件,為新的工程做好準(zhǔn)備,然后自動(dòng)打開該工程。為了方便
測試,拷貝幾個(gè)圖形文件到images文件夾中,文件類型可以是瀏覽器支持的任意圖形文件,如bmp、tif、gif等 。
在該工程中建立asp頁面,點(diǎn)擊菜單file | new ,在new 對話框中選files | active server page ,并指定其名字:
randimage.asp。visual interdev將會(huì)為我們產(chǎn)生一個(gè)空的框架,在其中用手工加入代碼。完成后的代碼如下
:
<%@ language="vbscript" %>
<html><head>
</head>
<body>
<h5>測試randimage 組件,隨機(jī)顯示一個(gè)圖形文件<h5>圖形文件路徑:
<%=server.mappath("images")%><br>
<%set
ox=server.createobject("randshowfile.randimage")’實(shí)例化組件ox.filepath=serve
r.mappath("images")
%>
<img src="<%=ox.filepath&ox.show%>">
<%set ox=nothing ’使用后釋放組件%>
</body>
</html>
由于web頁面使用的路徑(url)都是虛擬路徑(virtual directory),必須使用server.mappath()將其轉(zhuǎn)換到物理路徑
(physical directory)。例如,此處的圖形文件夾images的虛擬路徑是://servername/testrand image/images(其中
servername是你的web服務(wù)器的名字),其對應(yīng)的物理路徑是c:\inetpub\wwwroot\testrandimage\images 。如果不把
images映射到物理路徑則組件找不到該文件夾,無法正常工作。 代碼完成后測試之,注意到在每次打開或刷新該頁面時(shí),
會(huì)有一個(gè)不同的圖形顯示在上面。
5.結(jié)束語
使用ssc可以大大豐富web應(yīng)用的功能、提高編程效率;完成html或vbscript等不易完成的任務(wù);封裝特定的商業(yè)邏輯等。
server side component(以及activex)等組件的編程也發(fā)展成為一項(xiàng)有利可圖的事業(yè)。在internet上可以找到很多有用
的組件(免費(fèi)的或不免費(fèi)的),有興趣者可到www.15seconds.com、www.activeserverpages.com、www.serverobjects.com
等站點(diǎn)上查看。如果你有一
個(gè)新穎有用的組件,也可以發(fā)表在這些站點(diǎn)上,說不定你可以因此得到一筆可觀的收入呢。(重慶出版社電腦中心 陳剛)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
