為了能夠正常使用,必須把它們放到服務器上的一個虛擬應用程序內,并且把所提供的global.asa文件放到該應用程序的根目錄中。最簡單的辦法是把global.asa文件放到缺省Web網(wǎng)站的根目錄(缺省情況下是C:/InetPub/WWWRoot)中。
對任何已有的global.asa文件重命名是一個好辦法,可以在以后對該文件進行恢復。
1. 顯示Application集合的內容
ASPCounter對象是StaticObjects集合的一個成員(通過<OBJECT>元素進行定義),但是其余部份(由Server.CreateObject實例化)是Contents集合的成員。
可以看到使用global.asa例子網(wǎng)頁放到這些集合中的值,這在前面已經(jīng)看到:
<!-- Declare instance of the ASPCounter component with
application-level scope //-->
<OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin”
PROGID=”MSWC.Counters”>
</OBJECT>
...
...
<SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”>
Sub Application_onStart()
‘Create an instance of an ADO Connection with application-level scope
Set Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”)
Dim varArray(3) ‘Create a Variant array and fill it
varArray(0) = “This is a”
varArray(1) = “Variant array”
varArray(2) = “stored in the”
varArray(3) = “Application object”
Application(“Variant_Array”) = varArray ‘Store it in thd Application
Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string
Application(“Visit_Count”) = 0 ‘Set counter variable to zero
End Sub
...
...
</SCRIPT>
(1) 遍歷Contents集合的代碼
為了遍歷Contents集合,可使用一個For Each ... Next結構。集合中的每一項可以是一個簡單的Variant類型變量、一個Variant數(shù)組或者一個對象的引用。因為需要對每種類型的值進行不同的處理,所以就不得不對每一個進行檢查來判別其類型。
在VBScript中可使用VarType函數(shù)完成這個工作。這里使用IsObject和IsArray函數(shù)代替:
For Each objItem in Application.Contents
If IsObject(Application.Contents(objItem)) Then
Response.Write “Object reference: ‘” & objItem & “’
”
ElseIf IsArray(Application.Contents(objItem)) Then
Response.Write “Array: ‘” & objItem & “’ contents are:
”
VarArray = Application.Contents(objItem)
‘Note: the following only works with a one-dimensional array
For intLoop = 0 To UBound(varArray)
Response.Write “ Index(“ & intLoop & “) = “ & _
VarArray(intLoop) & “
”
Next
Else
Response.Write “Variable: ‘” & objItem & “’ = “ _
& Application.Contents(objItem) & “
”
End If
Next
注意程序如何從Application對象檢索該數(shù)組。將其分配給一個局部(Variant)變量,使用下面的語句:
varArray = Application.Contents(objItem)
使用UBound函數(shù)可以查找出數(shù)組的大小(元素的數(shù)量),這個值可以作為遍歷的終止條件:
For intLoop = 0 UBound(varArray)
這個例子是一維數(shù)組,并將只顯示這樣的一個數(shù)組的內容。可根據(jù)需要編輯代碼以處理多維數(shù)組,例如:
For intLoop = 0 To UBound(varArray)
IntNumberOfDimensions = UBound(varArray, 1)
For intDimension = 0 To intNumberOfDimensions
Response.Write “ Index(“ & intLoop & “) = “ _
& varArray(intLoop, intDimension)
Next
Response.Write “”
Next
(2) 遍歷StaticObjects集合的代碼
StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素聲明的對象引用。因為每個條目都是一個對象變量,可用簡單些的代碼對這個數(shù)組進行遍歷。我們將輸出對象的名字(在ID屬性中原有的定義):
For Each objItem in Application.StaticObjects
If IsObject(Application.StaticObjects(objItem)) Then
Response.Write “<OBJECT> element: ID=’” & objItem & “’
”
End If
Next
2、對集合的操作
1) 增加值到Contents集合
增加值到Contents集合的方法,與在global.asa網(wǎng)頁的腳本代碼中使用過的方法相同。允許把一個新的Variant值增加到Application對象中,并已有建議的名字和值(可根據(jù)需要進行編輯),單擊按鈕,重新載入這個網(wǎng)頁,把值增加到Application.Contents集合中,并且在列表中顯示。
增加新的Contents條目的代碼
所有的按鈕和其他HTML控件放置在示例網(wǎng)頁中的一個窗體上。ACTION設置了當前網(wǎng)頁的路徑,提交該窗體時,重新裝入。METHOD屬性為“POST”,所以控件中的值出現(xiàn)在Request.Form集合中。在以前的章節(jié)中采用過這兩種技術:
<FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”>
該窗體上的按鈕都是普通的HTML INPUT控件,具有相同的標題(三個空格)但名字不同。例如,創(chuàng)建第一個按鈕(把值增加到Application對象中)的代碼是:
<INPUT TYPE=”SUBMIT” NAME=”cmdAdd” VALUE=” ”>
重新載入該網(wǎng)頁時,檢查Request.Form集合,判定單擊的是哪個SUBMIT按鈕,并進行相應的處理。如果是增加一個值到Application對象的按鈕(該按鈕在HTML的<INPUT>元素中被命名為cmdAdd),使用下面的程序段:
If Len(Request.Form("cmdAdd")) Then
strVarName = Request.Form("txtVarName")
strVarValue = Request.Form("txtVarValue")
Application.Lock
Application("strVarName") = strVarValue
Application.Unlock
End If
注意程序如何使用Application.Lock和Application.Unlock方法,確保這些值不會因兩個用戶并發(fā)地訪問而產生混亂。如果只是對一個特定的值進行設置,一般不可能發(fā)生這種情況。但一直使用Lock和Unlock方法是明智的。
對任何已有的global.asa文件重命名是一個好辦法,可以在以后對該文件進行恢復。
1. 顯示Application集合的內容
ASPCounter對象是StaticObjects集合的一個成員(通過<OBJECT>元素進行定義),但是其余部份(由Server.CreateObject實例化)是Contents集合的成員。
可以看到使用global.asa例子網(wǎng)頁放到這些集合中的值,這在前面已經(jīng)看到:
<!-- Declare instance of the ASPCounter component with
application-level scope //-->
<OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin”
PROGID=”MSWC.Counters”>
</OBJECT>
...
...
<SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”>
Sub Application_onStart()
‘Create an instance of an ADO Connection with application-level scope
Set Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”)
Dim varArray(3) ‘Create a Variant array and fill it
varArray(0) = “This is a”
varArray(1) = “Variant array”
varArray(2) = “stored in the”
varArray(3) = “Application object”
Application(“Variant_Array”) = varArray ‘Store it in thd Application
Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string
Application(“Visit_Count”) = 0 ‘Set counter variable to zero
End Sub
...
...
</SCRIPT>
(1) 遍歷Contents集合的代碼
為了遍歷Contents集合,可使用一個For Each ... Next結構。集合中的每一項可以是一個簡單的Variant類型變量、一個Variant數(shù)組或者一個對象的引用。因為需要對每種類型的值進行不同的處理,所以就不得不對每一個進行檢查來判別其類型。
在VBScript中可使用VarType函數(shù)完成這個工作。這里使用IsObject和IsArray函數(shù)代替:
For Each objItem in Application.Contents
If IsObject(Application.Contents(objItem)) Then
Response.Write “Object reference: ‘” & objItem & “’
”
ElseIf IsArray(Application.Contents(objItem)) Then
Response.Write “Array: ‘” & objItem & “’ contents are:
”
VarArray = Application.Contents(objItem)
‘Note: the following only works with a one-dimensional array
For intLoop = 0 To UBound(varArray)
Response.Write “ Index(“ & intLoop & “) = “ & _
VarArray(intLoop) & “
”
Next
Else
Response.Write “Variable: ‘” & objItem & “’ = “ _
& Application.Contents(objItem) & “
”
End If
Next
注意程序如何從Application對象檢索該數(shù)組。將其分配給一個局部(Variant)變量,使用下面的語句:
varArray = Application.Contents(objItem)
使用UBound函數(shù)可以查找出數(shù)組的大小(元素的數(shù)量),這個值可以作為遍歷的終止條件:
For intLoop = 0 UBound(varArray)
這個例子是一維數(shù)組,并將只顯示這樣的一個數(shù)組的內容。可根據(jù)需要編輯代碼以處理多維數(shù)組,例如:
For intLoop = 0 To UBound(varArray)
IntNumberOfDimensions = UBound(varArray, 1)
For intDimension = 0 To intNumberOfDimensions
Response.Write “ Index(“ & intLoop & “) = “ _
& varArray(intLoop, intDimension)
Next
Response.Write “”
Next
(2) 遍歷StaticObjects集合的代碼
StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素聲明的對象引用。因為每個條目都是一個對象變量,可用簡單些的代碼對這個數(shù)組進行遍歷。我們將輸出對象的名字(在ID屬性中原有的定義):
For Each objItem in Application.StaticObjects
If IsObject(Application.StaticObjects(objItem)) Then
Response.Write “<OBJECT> element: ID=’” & objItem & “’
”
End If
Next
2、對集合的操作
1) 增加值到Contents集合
增加值到Contents集合的方法,與在global.asa網(wǎng)頁的腳本代碼中使用過的方法相同。允許把一個新的Variant值增加到Application對象中,并已有建議的名字和值(可根據(jù)需要進行編輯),單擊按鈕,重新載入這個網(wǎng)頁,把值增加到Application.Contents集合中,并且在列表中顯示。
增加新的Contents條目的代碼
所有的按鈕和其他HTML控件放置在示例網(wǎng)頁中的一個窗體上。ACTION設置了當前網(wǎng)頁的路徑,提交該窗體時,重新裝入。METHOD屬性為“POST”,所以控件中的值出現(xiàn)在Request.Form集合中。在以前的章節(jié)中采用過這兩種技術:
<FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”>
該窗體上的按鈕都是普通的HTML INPUT控件,具有相同的標題(三個空格)但名字不同。例如,創(chuàng)建第一個按鈕(把值增加到Application對象中)的代碼是:
<INPUT TYPE=”SUBMIT” NAME=”cmdAdd” VALUE=” ”>
重新載入該網(wǎng)頁時,檢查Request.Form集合,判定單擊的是哪個SUBMIT按鈕,并進行相應的處理。如果是增加一個值到Application對象的按鈕(該按鈕在HTML的<INPUT>元素中被命名為cmdAdd),使用下面的程序段:
If Len(Request.Form("cmdAdd")) Then
strVarName = Request.Form("txtVarName")
strVarValue = Request.Form("txtVarValue")
Application.Lock
Application("strVarName") = strVarValue
Application.Unlock
End If
注意程序如何使用Application.Lock和Application.Unlock方法,確保這些值不會因兩個用戶并發(fā)地訪問而產生混亂。如果只是對一個特定的值進行設置,一般不可能發(fā)生這種情況。但一直使用Lock和Unlock方法是明智的。