用ASP、VB和XML建立互聯(lián)網(wǎng)應(yīng)用程序(2)
2024-09-05 20:55:56
供稿:網(wǎng)友
,歡迎訪問網(wǎng)頁(yè)設(shè)計(jì)愛好者web開發(fā)。讓我們先分析一下客戶端/服務(wù)器應(yīng)用程序。在一個(gè)標(biāo)準(zhǔn)的客戶端/服務(wù)器應(yīng)用程序中,在應(yīng)用程序開始時(shí),你能夠初始化數(shù)據(jù)庫(kù)連接字符串,這就意味著,客戶有使用數(shù)據(jù)庫(kù)連接字符串的權(quán)利,這包括用戶名和口令。但是客觀情況如果不允許你在網(wǎng)絡(luò)上發(fā)送這些信息的話,你就必需在不聯(lián)接數(shù)據(jù)庫(kù)的情況下直接從客戶端取得數(shù)據(jù)發(fā)送給客戶。那么解決方案之一就是在服務(wù)器上創(chuàng)建一個(gè)asp頁(yè)(在本例中稱為getdata.asp)接收特定格式的post數(shù)據(jù),它要求一個(gè)包含xml字符串,用來創(chuàng)建ado對(duì)象并運(yùn)行存儲(chǔ)過程或動(dòng)態(tài)sql語(yǔ)句命令。如果信息有效的話,getdata.asp執(zhí)行存儲(chǔ)過程,并返回一個(gè)xml格式的數(shù)據(jù)集、返回值列表或錯(cuò)誤頁(yè)面信息的xml字符串。對(duì)于返回?cái)?shù)據(jù)的命令,客戶端要么重新實(shí)例化要么返回值或使用xml dom(document object model文檔對(duì)象模型)格式的錯(cuò)誤頁(yè)面。
好,下面就讓我們來討論一下如何實(shí)現(xiàn)這個(gè)頁(yè)面吧!
getdata.asp頁(yè)面首先使用一個(gè)domdocument對(duì)象來保存客戶端發(fā)送的數(shù)據(jù):
'創(chuàng)建domdocument對(duì)象
set xml = server.createobject ("msxml2.domdocument")
xml.async = false
然后,它裝載post數(shù)據(jù)
'裝載post數(shù)據(jù)
xml.load request
if xml.parseerror.errorcode <> 0 then
call responseerror ("不能裝載xml信息。" & "description: " & xml.parseerror.reason & "<br>line: " & xml.parseerror.line)
end if
它能夠返回commandtext元素值和returndata或returnvalue元素值。下面我只給出返回commandtext元素值的代碼,其余代碼請(qǐng)參看我下面所附的源程序。
set n = xml.selectsinglenode("command/commandtext")
if n is nothing then
call responseerror ("缺少 <sp_name> 參數(shù)。")
else sp_name = n.text
end if
接著,應(yīng)該讓頁(yè)面創(chuàng)建一個(gè)command對(duì)象,讀入所有<param>元素,并且為request中的每一個(gè)元素創(chuàng)建一個(gè)參數(shù)。最后,讓頁(yè)面打開一個(gè)連接使用存儲(chǔ)過程adexecutenorecords選項(xiàng)來執(zhí)行request。
set conn = server.createobject("adodb.connection")
conn.mode=admodereadwrite
conn.open application("connectionstring")
set cm.activeconnection=conn
' 返回?cái)?shù)據(jù)
if not returnsdata then
cm.execute
else
set r = server.createobject("adodb.recordset")
r.cursorlocation = aduseclient
r.open cm, ,adopenstatic, adlockreadonly
end if
如果能夠返回?cái)?shù)據(jù)的話,那么returndata變量就為真值,并且把結(jié)果數(shù)據(jù)集返回到客戶端,仍然是一個(gè)xml文檔。
if returnsdata then
r.save response, adpersistxml
if err.number <> 0 then
call responseerror ("數(shù)據(jù)集發(fā)生存儲(chǔ)錯(cuò)誤" & "在命令'" & commandtext & "': " & err.description)
response.end
end if
如果輸出參數(shù)返回值,那么這個(gè)頁(yè)面將返回一個(gè)包含這些值的xml字符串。文檔的根元素是一個(gè)<values>標(biāo)記,每一個(gè)返回值都有其相應(yīng)的子元素,如果發(fā)生任何錯(cuò)誤,頁(yè)面都會(huì)格式化并返回一個(gè)包含錯(cuò)誤信息的xml字符串:
sub responseerror(sdescription)
response.write "<response><data>錯(cuò)誤: " & sdescription & "</data></response>"
response.end
end sub