web|window|程式|頁面|執行
現在許多公司都面臨一個難題:如何在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