在Web頁面中執(zhí)行Windows程序 現(xiàn)在許多公司都面臨一個(gè)難題:如何在Web環(huán)境中執(zhí)行存在的Windows應(yīng)用程序。這里就介紹實(shí)現(xiàn)這個(gè)功能的技術(shù),它爭取對代 碼做最小的改變,完成在Windows環(huán)境中應(yīng)做的一切。
現(xiàn)存的Windows應(yīng)用程序
這里想要在Web中執(zhí)行的Windows例子程序是非常簡單的,它是用VB編寫的,其中有一個(gè)表單。運(yùn)行時(shí),在表單上顯示雇員的信 息,這些信息來源于Access數(shù)據(jù)庫的一個(gè)表。表單上設(shè)有First、Next、Previous 和 Last按鈕,從而允許用戶瀏覽記錄。同時(shí), 還可以通過按鈕Add、Delete 和 Update來改變數(shù)據(jù)。
這個(gè)程序通過一個(gè)COM類來與數(shù)據(jù)庫通信,它有下面的方法:
AddEmployee() 在表中添加一個(gè)記錄,保存新雇員的信息 UpdateEmployee() 更新一個(gè)記錄 DeleteEmployee() 刪除一個(gè)記錄 GetEmployees() 獲取一個(gè)雇員的信息
程序正常運(yùn)行時(shí),瀏覽器顯示如下:
開發(fā)Web應(yīng)用程序
在傳統(tǒng)的web應(yīng)用程序中,大多數(shù)的處理都是在服務(wù)器端完成的。這里,我們將嘗試在客戶端做一些處理,以減少服務(wù)器上的工 作量。也就是,讓客戶端完成顯示信息的處理工作,并將商業(yè)規(guī)則和數(shù)據(jù)庫存取留給服務(wù)器端。這就象一個(gè)n層處理模型。
當(dāng)用戶需要訪問另一個(gè)不同的數(shù)據(jù)時(shí),我們也不想從服務(wù)器上再調(diào)入整個(gè)web頁面,因此,需要找到一個(gè)web客戶端在后臺(tái)與 web服務(wù)器交流信息的方法。這個(gè)例子中,我們使用了微軟公司的XMLHTTP COM對象組件,它是隨Internet Explorer 5.0而來 的。當(dāng)然,也可以編寫一個(gè)功能類似的Java applet來克服這個(gè)局限。
服務(wù)器端的代碼
讓我們從研究VB應(yīng)用程序的COM類到Web的每一個(gè)方法開始,這可以通過編寫ASP頁面來調(diào)用COM類中的每個(gè)方法實(shí)現(xiàn) (AddEmployee.asp, UpdateEmployee.asp, DeleteEmployee.asp, GetEmployee.asp)。 明白了這些,就能夠在Web中存取COM 類方法了。
ASP頁面應(yīng)該能夠接受與COM類一樣的參數(shù),這些頁面向原始的COM類發(fā)送調(diào)用。這里主要的區(qū)別就是所有的輸出是以XML格式 的。我們使用另外一個(gè)叫XMLConverter的COM類,轉(zhuǎn)換方法的輸出為XML格式。XMLConverter的代碼包含在下載文件中,它有一個(gè) 函數(shù),能夠接受一個(gè)ADO記錄集做為參數(shù),并且轉(zhuǎn)換為一個(gè)XML文檔。實(shí)現(xiàn)這個(gè)目的的函數(shù)例子可以從Internet上很容易地找到,比 如:
http://www.vbxml.com/xml/guides/developers/ado_persist_xml.asp
我們也許曾經(jīng)使用過ADO記錄集的Save函數(shù),加上adPersistXML,來實(shí)現(xiàn)按照xml格式保存,但是在這里,為了簡單起見,我 們?nèi)允褂镁幹频暮瘮?shù)。
下面的代碼來自GetEmployees.asp,它執(zhí)行GetEmployees方法,從而可以讓你清晰地看到這種技術(shù):
<SCRIPT LANGUAGE=vbscript RUNAT=Server> 'Declare the above described XMLConverter Dim objXMLConverter
'Create the XMLConverter object on the web server machine Set objXMLConverter = Server.CreateObject("XMLConverter.clsXMLConverter")
'Declare the above described EmployeeMgr object Dim objEmployeeMgr
'Create the EmployeeMgr object on the web server machine Set objEmployeeMgr = Server.CreateObject("EmployeeMgr.clsEmployeeMgr")
'Declare a String varaible Dim strXML
現(xiàn)在調(diào)用Employees對象的Employees()方法,它將返回一個(gè)ADO記錄集對象,我們將這個(gè)對象傳遞給XMLConverter對象的 xmlRecordset()方法,xmlRecordset()負(fù)責(zé)將ADO記錄集轉(zhuǎn)換為XML文檔。最后,我們?nèi)』豖ML文檔,并將它存入strXML字符 串變量中:
strXML = objXMLConverter.xmlRecordset(objEmployeeMgr.GetEmployees)
'Destroy the EmployeeMgr object Set objEmployeeMgr = Nothing
'Destroy the XMLConverter object Set objXMLConverter = Nothing
'Write the XML Document as the response Response.Write strXML </SCRIPT>
然后,用同樣的方式來創(chuàng)建頁面AddEmplyee.asp、DeleteEmployee.asp和UpdateEmployee.asp。
客戶端的代碼
現(xiàn)在準(zhǔn)備編寫客戶端代碼,首先,讓我們仔細(xì)看看VB應(yīng)用程序在調(diào)用后如何顯示信息。在VB表單的On_Load方法(參見下面的 代碼)中,我們調(diào)用了COM對象組件GetEmployees方法,這個(gè)方法返回一個(gè)附帶雇員信息的ADO記錄集。ADO記錄集的MoveFirst ()、MoveNext()以及 MoveLast() 方法建立了記錄瀏覽的方法。
Private Sub Form_Load() 'Create the EmployeeMgr Object Set objEmplyeeMgr = New clsEmployeeMgr
'Obtain the Employee Records in a ADODB.Recordset Set rst = objEmplyeeMgr.GetEmployees rst.MoveFirst DisplayCurrentRecord End Sub
在這種情況下,我們有一個(gè)ASP頁面GetEmployees.asp,它給出了做為XML文檔的信息。所以我們將在web客戶端建立一個(gè) XMLDOM對象,并且調(diào)入由GetEmployees.asp提供的信息。在這個(gè)例子中,我們使用Microsoft DOM XML來解析。關(guān)于DOM XML解 析的完整文檔,請參考MSDN有關(guān)文章,比如 XML DOM Objects。
另一個(gè)較好的解決方法是使用純Java/JavaScript,它同時(shí)可以在非Internet Explorer的瀏覽器上應(yīng)用。這里,我們?nèi)允褂?br>XMLHTTP對象,它可以讓web客戶端建立一個(gè)到web服務(wù)器的HTTP請求。關(guān)于對XML HTTP的詳細(xì)描述,請參考MSDN上的文檔。
//Create an XMLDOM on the Web Client machine var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
// node pointing at Root node of the XML Document var nodeRoot;
// node to point at Current Record var nodeCurrentRecord;
// Integer pointing at the current Record number var nCurrentIndex = 0;
//Create the Microsoft XMLHTTP object on the web client machine //This would have to be present and registered on the client machine //Installing Internet Explorer 5.0 satisfies this requirement var objHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
//Open a http connection to the URL in strURL objHTTPRequest.Open ("GET", strURL, false, null, null);
//Send the request objHTTPRequest.send();
//Obtain the response received to the xmlResponse variable //This response would be the XML document returned by the web server var xmlResponse = objHTTPRequest.responseText
//Since the response is an XML document we can load it to an xmlDoc object xmlDoc.loadXML (xmlResponse);
//Set nodeRoot to point at the root of the xml document nodeRoot = xmlDoc.documentElement;
從上面我們了解了XML文檔的結(jié)構(gòu),現(xiàn)在可以仔細(xì)研究XML文檔對象了。我們將編寫一個(gè)客戶端的JavaScript函數(shù) rstMoveFirst(),它可以移動(dòng)當(dāng)前記錄指針到第1條,這與ADO記錄集的MoveFirst方法類似:
function rstMoveFirst() { //Error trap for empty record set if (nodeRoot.childNodes.length; < 1) {
//If the root node does not have any child nodes then there are //no "records" return false; } nCurrentIndex = 0;
//Set the nodeCurrentRecord to point at the 0th child of the //XML Document. The 0th child would be the first record. // nodeRoot is the XML document? documentElement nodeCurrentRecord = nodeRoot.childNodes(nCurrentIndex);
//Return Success return true; }
同樣,我們可以編寫rstMoveNext()和 rstMoveLast()函數(shù),通過編寫這些代碼,我們將能仔細(xì)地了解XML文檔元素。而且, 再編寫一個(gè)類似于ADO記錄集upadte方法的函數(shù)。
現(xiàn)在我們在客戶機(jī)上創(chuàng)建了一個(gè)假冒的ADO記錄集對象,因此就可以象在VB應(yīng)用程序中一樣來處理這些“記錄集”。
有了這些函數(shù),剩下的就是編寫用戶界面,這就象在VB應(yīng)用程序中一樣。比如,在VB應(yīng)用程序中,“移動(dòng)到第1條記錄”后面 的代碼是:
Private Sub btnFirst_Click() If Not (rst.EOF And rst.BOF) Then
'Move to the first record rst.MoveFirst
'DisplayCurrentRecord is a function that display the current 'records information DisplayCurrentRecord
End If End Sub
在web應(yīng)用程序中,相應(yīng)的代碼是:
function btnFirstClick() { 'Move to the first record in the recordset 'Note that our rstMoveFirst returns True if 'it was successful and false if EOF and BOF if (rstMoveFirst()) { 'Here DisplayCurrentRecord is client side JavaScript 'function that display the current records information on 'the the screen DisplayCurrentRecord(); } }
當(dāng)需要更新實(shí)際的數(shù)據(jù)庫時(shí),就發(fā)送更新信息給UpdateEmployee.asp,這個(gè)頁面將通過COM對象的UpdateEmployee方法來更 新數(shù)據(jù)庫。上面描述的應(yīng)用程序,輸出到web上,將顯示如下圖:
結(jié)論
在web應(yīng)用中,要建立適當(dāng)?shù)挠?jì)劃來轉(zhuǎn)換ADO記錄集為XML文檔。一旦定義明確了,象那樣標(biāo)準(zhǔn)的應(yīng)用將很好實(shí)現(xiàn)。另外一個(gè)我 們想研究的領(lǐng)域是客戶端記錄集的操縱函數(shù)(就是rst*函數(shù))。我們可以編寫Java applet來處理這些記錄集操縱函數(shù),從而在客 戶端建立了Java記錄集對象。這種方法將是很面向?qū)ο蟮囊环N處理方法。
點(diǎn)擊此處下載本文相關(guān)資料: http://www.asptoday.com/articles/images/20000602.zip
|