top
Loading...
在Web頁面中執行Windows程序
現在許多公司都面臨一個難題:如何在Web環境中執行存在的Windows應用程序。這里就介紹實現這個功能的技術,它爭取對代
碼做最小的改變,完成在Windows環境中應做的一切。

現存的Windows應用程序

這里想要在Web中執行的Windows例子程序是非常簡單的,它是用VB編寫的,其中有一個表單。運行時,在表單上顯示雇員的信
息,這些信息來源于Access數據庫的一個表。表單上設有First、Next、Previous 和 Last按鈕,從而允許用戶瀏覽記錄。同時,
還可以通過按鈕Add、Delete 和 Update來改變數據。

這個程序通過一個COM類來與數據庫通信,它有下面的方法:

AddEmployee() 在表中添加一個記錄,保存新雇員的信息
UpdateEmployee() 更新一個記錄
DeleteEmployee() 刪除一個記錄
GetEmployees() 獲取一個雇員的信息

程序正常運行時,瀏覽器顯示如下:


開發Web應用程序

在傳統的web應用程序中,大多數的處理都是在服務器端完成的。這里,我們將嘗試在客戶端做一些處理,以減少服務器上的工
作量。也就是,讓客戶端完成顯示信息的處理工作,并將商業規則和數據庫存取留給服務器端。這就象一個n層處理模型。

當用戶需要訪問另一個不同的數據時,我們也不想從服務器上再調入整個web頁面,因此,需要找到一個web客戶端在后臺與
web服務器交流信息的方法。這個例子中,我們使用了微軟公司的XMLHTTP COM對象組件,它是隨Internet Explorer 5.0而來
的。當然,也可以編寫一個功能類似的Java applet來克服這個局限。

服務器端的代碼

讓我們從研究VB應用程序的COM類到Web的每一個方法開始,這可以通過編寫ASP頁面來調用COM類中的每個方法實現
(AddEmployee.asp, UpdateEmployee.asp, DeleteEmployee.asp, GetEmployee.asp)。 明白了這些,就能夠在Web中存取COM
類方法了。

ASP頁面應該能夠接受與COM類一樣的參數,這些頁面向原始的COM類發送調用。這里主要的區別就是所有的輸出是以XML格式
的。我們使用另外一個叫XMLConverter的COM類,轉換方法的輸出為XML格式。XMLConverter的代碼包含在下載文件中,它有一個
函數,能夠接受一個ADO記錄集做為參數,并且轉換為一個XML文檔。實現這個目的的函數例子可以從Internet上很容易地找到,比
如:

http://www.vbxml.com/xml/guides/developers/ado_persist_xml.asp

我們也許曾經使用過ADO記錄集的Save函數,加上adPersistXML,來實現按照xml格式保存,但是在這里,為了簡單起見,我
們仍使用編制的函數。

下面的代碼來自GetEmployees.asp,它執行GetEmployees方法,從而可以讓你清晰地看到這種技術:

<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


現在調用Employees對象的Employees()方法,它將返回一個ADO記錄集對象,我們將這個對象傳遞給XMLConverter對象的
xmlRecordset()方法,xmlRecordset()負責將ADO記錄集轉換為XML文檔。最后,我們取回XML文檔,并將它存入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>


然后,用同樣的方式來創建頁面AddEmplyee.asp、DeleteEmployee.asp和UpdateEmployee.asp。

客戶端的代碼

現在準備編寫客戶端代碼,首先,讓我們仔細看看VB應用程序在調用后如何顯示信息。在VB表單的On_Load方法(參見下面的
代碼)中,我們調用了COM對象組件GetEmployees方法,這個方法返回一個附帶雇員信息的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


在這種情況下,我們有一個ASP頁面GetEmployees.asp,它給出了做為XML文檔的信息。所以我們將在web客戶端建立一個
XMLDOM對象,并且調入由GetEmployees.asp提供的信息。在這個例子中,我們使用Microsoft DOM XML來解析。關于DOM XML解
析的完整文檔,請參考MSDN有關文章,比如 XML DOM Objects。

另一個較好的解決方法是使用純Java/JavaScript,它同時可以在非Internet Explorer的瀏覽器上應用。這里,我們仍使用
XMLHTTP對象,它可以讓web客戶端建立一個到web服務器的HTTP請求。關于對XML HTTP的詳細描述,請參考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文檔的結構,現在可以仔細研究XML文檔對象了。我們將編寫一個客戶端的JavaScript函數
rstMoveFirst(),它可以移動當前記錄指針到第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()函數,通過編寫這些代碼,我們將能仔細地了解XML文檔元素。而且,
再編寫一個類似于ADO記錄集upadte方法的函數。

現在我們在客戶機上創建了一個假冒的ADO記錄集對象,因此就可以象在VB應用程序中一樣來處理這些“記錄集”。

有了這些函數,剩下的就是編寫用戶界面,這就象在VB應用程序中一樣。比如,在VB應用程序中,“移動到第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應用程序中,相應的代碼是:

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();
}
}

當需要更新實際的數據庫時,就發送更新信息給UpdateEmployee.asp,這個頁面將通過COM對象的UpdateEmployee方法來更
新數據庫。上面描述的應用程序,輸出到web上,將顯示如下圖:


結論

在web應用中,要建立適當的計劃來轉換ADO記錄集為XML文檔。一旦定義明確了,象那樣標準的應用將很好實現。另外一個我
們想研究的領域是客戶端記錄集的操縱函數(就是rst*函數)。我們可以編寫Java applet來處理這些記錄集操縱函數,從而在客
戶端建立了Java記錄集對象。這種方法將是很面向對象的一種處理方法。

點擊此處下載本文相關資料:
http://www.asptoday.com/articles/images/20000602.zip

北斗有巢氏 有巢氏北斗