(1) 遍歷Contents集合的代碼 為了遍歷Contents集合,可使用一個(gè)For Each ... Next結(jié)構(gòu)。集合中的每一項(xiàng)可以是一個(gè)簡單的Variant類型變量、一個(gè)Variant數(shù)組或者一個(gè)對(duì)象的引用。因?yàn)樾枰獙?duì)每種類型的值進(jìn)行不同的處理,所以就不得不對(duì)每一個(gè)進(jìn)行檢查來判別其類型。 在VBScript中可使用VarType函數(shù)完成這個(gè)工作。這里使用IsObject和IsArray函數(shù)代替: For Each objItem in application.Contents If IsObject(Application.Contents(objItem)) Then Response.Write “Object reference: ‘” & objItem & “’<BR>” ElseIf IsArray(Application.Contents(objItem)) Then Response.Write “Array: ‘” & objItem & “’ contents are:<BR>” 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) & “<BR>” Next Else Response.Write “Variable: ‘” & objItem & “’ = “ _ & Application.Contents(objItem) & “<BR>” End If Next 注意程序如何從Application對(duì)象檢索該數(shù)組。將其分配給一個(gè)局部(Variant)變量,使用下面的語句: varArray = Application.Contents(objItem) 使用UBound函數(shù)可以查找出數(shù)組的大小(元素的數(shù)量),這個(gè)值可以作為遍歷的終止條件: For intLoop = 0 UBound(varArray) 這個(gè)例子是一維數(shù)組,并將只顯示這樣的一個(gè)數(shù)組的內(nèi)容。可根據(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 “<BR>” Next (2) 遍歷StaticObjects集合的代碼 StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素聲明的對(duì)象引用。因?yàn)槊總€(gè)條目都是一個(gè)對(duì)象變量,可用 簡單些的代碼對(duì)這個(gè)數(shù)組進(jìn)行遍歷。我們將輸出對(duì)象的名字(在ID屬性中原有的定義): For Each objItem in Application.StaticObjects If IsObject(Application.StaticObjects(objItem)) Then Response.Write “<OBJECT> element: ID=’” & objItem & “’<BR>” End If Next 1. 增加值到Contents集合 增加值到Contents集合的方法,與在global.asa網(wǎng)頁的腳本代碼中使用過的方法相同。示例網(wǎng)頁允許把一個(gè)新的Variant值 增加到Application對(duì)象中,并已有建議的名字和值(可根據(jù)需要進(jìn)行編輯),如圖3-15所示:
圖3-16 顯示Contents集合內(nèi)容的屏幕 增加新的Contents條目的代碼 所有的按鈕和其他HTML控件放置在示例網(wǎng)頁中的一個(gè)窗體上。ACTION設(shè)置了當(dāng)前網(wǎng)頁的路徑,提交該窗體時(shí),重新 裝入。METHOD屬性為“POST”,所以控件中的值出現(xiàn)在Request.Form集合中。在以前的章節(jié)中采用過這兩種技術(shù): <FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”> 該窗體上的按鈕都是普通的HTML INPUT控件,具有相同的標(biāo)題(三個(gè)空格)但名字不同。例如,創(chuàng)建第一個(gè)按鈕(把值增加到Application對(duì)象中)的代碼是: <INPUT TYPE=”SUBMIT” NAME=”cmdAdd” VALUE=” ”> 重新載入該網(wǎng)頁時(shí),檢查Request.Form集合,判定單擊的是哪個(gè)SUBMIT按鈕,并進(jìn)行相應(yīng)的處理。如果是增加一個(gè)值到Application對(duì)象的按鈕(該按鈕在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方法,確保這些值不會(huì)因兩個(gè)用戶并發(fā)地訪問而產(chǎn)生混 亂。如果只是對(duì)一個(gè)特定的值進(jìn)行設(shè)置,一般不可能發(fā)生這種情況。但一直使用Lock和Unlock方法是明智的。 2. Contents集合中刪除值 在例子網(wǎng)頁的底部有兩個(gè)按鈕,如圖3-17所示:
圖3-17 顯示在網(wǎng)頁底部的兩個(gè)按鈕 這兩個(gè)按鈕允許從Application.Contents集合中刪除值。第一個(gè)按鈕從集合中刪除單個(gè)的指定值,下拉列表框顯示的是 Contents集合值的名字的列表(記住,不能從StaticObjects集合中刪除值,因?yàn)樗鼈兪庆o態(tài)的)。 通過遍歷Contents集合(如前面我們所做的)執(zhí)行asp網(wǎng)頁時(shí),創(chuàng)建該列表。但是,我們僅收集每項(xiàng)的名字并把它們放到 <SELECT>列表元素內(nèi)的<OPTION>元素中: … <SELECT NAME=”lstRemove” SIZE=”1”> <% For Each objItem in Application.Contents Response.Write “<OPTION>” & objItem & “</OPTION>” Next &> </SELECT> … 該ASP代碼執(zhí)行以后,在瀏覽器中看到的結(jié)果是: <SELECT NAME=”lstRemove” SIZE=”1”> <OPTION>ADOConnection</OPTION> <OPTION>Variant_Array</OPTION> <OPTION>Start_Time</OPTION> <OPTION>Visit_Count</OPTION> <OPTION>My_New_Value</OPTION> </SELECT> (1) 刪除單個(gè)值 當(dāng)單擊按鈕刪除單個(gè)值時(shí),該窗體再次提交給相同的網(wǎng)頁,但是這一次將查找名為cmdRemoveThis的SUBMIT按鈕。如果存在(即單擊了這個(gè)按鈕),則使用列表框的值,調(diào)用Application.Contents集合的Remove方法: If Len(Request.Form("cmdRemoveThis")) Then strToRemove = Request.Form("lstRemove") Response.Write "strToRemove = " & strToRemove Application.Lock Application.Contents.Remove(strToRemove) Application.Unlock End If 注意這是Contents集合的一個(gè)方法,而不是Application對(duì)象的。語法是Application.Contents.Remove,而不是 Application.Remove。 從Contents集合中刪除Start_Time值的結(jié)果如圖3-18所示:
圖3-18 刪除Start_Time值后的屏幕 (2) 刪除所有的值 如果單擊三個(gè)SUBMIT類型按鈕中的最后一個(gè)(如圖3-18所示),該網(wǎng)頁中的代碼將檢測到單擊的按鈕為cmdRemoveAll,將 執(zhí)行Application.Contents集合的RemoveAll方法: If Len(Request.Form("cmdRemoveAll")) Then Application.Lock Application.Contents.RemoveAll Application.Unlock End If 再次提醒,這是Contents集合的一個(gè)方法,而不是Application。語法是Application.Contents.RemoveAll,而不是 Application.RemoveAll。 圖3-19所示的是從Contents集合中刪除所有值的結(jié)果(記住在運(yùn)行時(shí)間不能從StaticObjects集合刪除項(xiàng)):
圖3-20 Session對(duì)象顯示屬性的屏幕 下面是例子中使用的golbal.asa文件的相關(guān)段落,它把缺省值增加到圖3-20所示的屏幕上所看到的會(huì)話中: ... <!-- Declare instance of the ASPContentLink component with session-level scope //--> <OBJECT ID="ASPContentLink" RUNAT="Server" SCOPE="Session" PROGID="MSWC.NextLink"> </OBJECT>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> ... ... Sub Session_onStart()
'Create an instance of the AdRotator component with session-level scope Set Session("ASPAdRotator") = Server.CreateObject("MSWC.AdRotator") 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) = "Session object" Session("Variant_Array") = varArray 'Store it in the Session Session("Start_Time") = CStr(Now) 'Store the date/time as a string
'We can access the contents of the Request and Response in a Session_onStart 'event handler for the page that initiated the session. This is the *only* 'place that the ASP page context is available like this. 'as an example, we can get the IP address of the user: Session("Your_IP_Address") = Request.ServerVariables("REMOTE_ADDR")
Application.Lock 'Prevent concurrent updates intVisits = Application("Visit_Count") + 1 'Increment counter variable Application("Visit_Count") = intVisits 'Store back in Application Application.Unlock 'Release lock on Application
End Sub ... ... </SCRIPT> 遍歷Contents和StaticObjects集合的代碼與前面在Application對(duì)象示例中使用的代碼一樣,只不過這里引用了Session.Contents和Session.StaticObjects集合,而不是Application.Contents和Appliction.StaticObjects集合。 靠近頁面底部的按鈕的功能是把值增加到Session.Contents集合和從Session.Contents集合刪除值。這些按鈕與在Application對(duì)象示例頁面中相應(yīng)的按鈕工作方式相同,這里訪問的是Session.Contents集合,以及相應(yīng)的Remove和 RemoveAll方法。我們不再重復(fù)解釋。 2. 終止一個(gè)用戶會(huì)話 在Session對(duì)象頁面的底部有一個(gè)按鈕,該按鈕終止當(dāng)前的用戶會(huì)話,這通過調(diào)用Session對(duì)象的Abandon方法實(shí)現(xiàn)。它與其余的HTML控件在相同的窗體上,名為cmdAbandon。當(dāng)該窗體再次被提交給這個(gè)網(wǎng)頁時(shí),在Request.Form集合中查找這個(gè)值(如同在Application對(duì)象例子中做的一樣)。如發(fā)現(xiàn)這個(gè)值,則將該用戶重定向到另一個(gè)網(wǎng)頁: If Len(Request.Form("cmdAbandon")) Then Response.Clear Response.Redirect "abandon.asp" Response.End End If 新的頁面名為abandon.asp,十分簡單,除了創(chuàng)建消息的文本和HTML以外只有如下的代碼: <% Session.Abandon %> 該網(wǎng)頁的其余部分只是一個(gè)包含單個(gè)SUBMIT按鈕的窗體。注意如何使用來自Request.ServerVariables集合的引用網(wǎng)頁 (HTTP_REFERER)的URL的值,以確保重新載入前一個(gè)網(wǎng)頁(Session對(duì)象例子網(wǎng)頁): <FORM ACTION="<% = Request.ServerVariables("HTTP_REFERER") %>" METHOD="POST"> <P><DIV CLASS="subhead">Your Session Has Been Terminated</DIV> A new <B>Session</B> will be started when you load another<BR> ASP page. It will contain any values that are defined in<BR> the <B>global.asa</B> file for this application.<P> <INPUT TYPE="SUBMIT" NAME="cmdOK" VALUE=" "> Return to the previous page<P> </FORM> 結(jié)果如圖3-21所示。這時(shí),當(dāng)前的用戶會(huì)話已經(jīng)被終止(放棄),并且該用戶不能引用原先的Session集合或?qū)傩缘膬?nèi)容: