asp服務器組件
asp(active server page)是當今開發交互式web頁面、web數據庫應用最強大的技術。在其中可以混用html、dhtml、
activex、vbscript或javascript。當這些技術都無法奏效時(例如進行高密度的數學運算、封裝特定的數據庫處理邏輯
等),可以使用服務器組件(server sidecomponent)進一步擴展asp的能力。
server sidecomponent實際上是運行在服務器上的一個dll,它可以完成常規dll所能勝任的任何任務。不同之處是:它由
asp頁面調用、并以web頁面為交互對象,讀入用戶的輸入(web頁面上各輸入域的值),處理后返回結果到web頁面。這些
交互當然都要通過web服務器作為中介??梢杂胿b、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
等站點上查看。如果你有一
個新穎有用的組件,也可以發表在這些站點上,說不定你可以因此得到一筆可觀的收入呢。(重慶出版社電腦中心 陳剛)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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