集 合 | 說 明 |
Contents | 沒有使用<OBJECT>元素定義的存儲于Application對象中的所有變量(及它們的值)的一個集合。包括Variant數組和Variant類型對象實例的引用 |
StaticObjects | 使用<OBJECT>元素定義的存儲于Application對象中的所有變量(及它們的值)的一個集合 |
方 法 | 說 明 |
Contents.Remove(“variable_name”) | 從Application.Content集合中刪除一個名為variable_name的變量 |
Contents.RemoveAll() | 從Application.Content集合中刪除所有變量 |
Lock() | 鎖定Application對象,使得只有當前的ASP頁面對內容能夠進行訪問。用于確保通過允許兩個用戶同時地讀取和修改該值的方法而進行的并發操作不會破壞內容 |
Unlock() | 解除對在Application對象上的ASP網頁的鎖定 |
事 件 | 說 明 |
OnStart | 當ASP啟動時觸發,在用戶請求的網頁執行之前和任何用戶創建Session對象之前。用于初始化變量、創建對象或運行其他代碼 |
OnEnd | 當ASP應用程序結束時觸發。在最后一個用戶會話已經結束并且該會話的OnEnd事件中的所有代碼已經執行之后發生。其結束時,應用程序中存在的所有變量被取消 |
集 合 | 說 明 |
Contents | 存儲于這個特定Session對象中的所有變量和其值的一個集合,并且這些變量和值沒有使用<OBJECT>元素進行定義。包括Variant數組和Variant類型對象實例的引用 |
StaticObjects | 通過使用<OBJECT>元素定義的、存儲于這個Session對象中的所有變量的一個集合 |
屬 性 | 說 明 |
CodePage | 讀/寫。整型。定義用于在瀏覽器中顯示頁內容的代碼頁(Code Page)。代碼頁是字符集的數字值,不同的語言和場所可能使用不同的代碼頁。例如,ANSI代碼頁1252用于美國英語和大多數歐洲語言。代碼頁932用于日文字 |
LCID | 讀/寫。整型。定義發送給瀏覽器的頁面地區標識(LCID)。LCID是唯一地標識地區的一個國際標準縮寫,例如,2057定義當前地區的貨幣符號是’£’。LCID也可用于FormatCurrency等語句中,只要其中有一個可選的LCID參數。LCID也可在ASP處理指令<%…%>中設置,并優先于會話的LCID屬性中的設置。本章后面提供一個ASP處理指令的列表 |
SessionID | 只讀。長整型。返回這個會話的會話標識符,創建會話時,該標識符由服務器產生。只在父Application對象的生存期內是唯一的,因此當一個新的應用程序啟動時可重新使用 |
Timeout | 讀/寫。整型。為這個會話定義以分鐘為單位的超時周期。如果用戶在超時周期內沒有進行刷新或請求一個網頁,該會話結束。在各網頁中根據需要可以修改。缺省值是10min。在使用率高的站點上該時間應更短 |
方 法 | 說 明 |
Contents.Remove(“variable_name”) | 從Session.Content集合中刪除一個名為variable_name的變量 |
Contents.RemoveAll() | 從Session.Content集合中刪除所有變量 |
Abandon() | 當網頁的執行完成時,結束當前用戶會話并撤消當前Session對象。但即使在調用該方法以后,仍可訪問該頁中的當前會話的變量。當用戶請求下一個頁面時將啟動一個新的會話,并建立一個新的Session對象(如果存在的話) 注意,在運行期間不能從Session.StaticObjects集合中刪除變量。 |
事 件 | 說 明 |
OnStart | 當ASP用戶會話啟動時觸發,在用戶請求的網頁執行之前。用于初始化變量、創建對象或運行其他代碼。 |
OnEnd | 當ASP用戶會話結束時觸發。從用戶對應用程序的最后一個頁面請求開始,如果已經超出預定的會話超時周期則觸發該事件。當會話結束時,取消該會話中的所有變量。在代碼中使用Abandon方法結束ASP用戶會話時,也觸發該事件 |
使用Application和Session的事件
ASP的Application和Session對象體現了其他ASP內置對象所沒有的特征——事件。然而,正像在前面的對象成員表中看到的那樣,這些都是ASP會話和應用程序的工作相聯系的事件。
1. Application和Session的事件處理器
每當一個應用程序或會話啟動或結束時,ASP觸發一個事件??梢酝ㄟ^在一個特殊的文件中編寫普通的腳本代碼來檢測和應答這些事件,這個文件名為global.asa,位于一個應用程序的根目錄中(對于缺省的Web網站是/InetPub/WWWRoot目錄,或是作為一個實際應用程序定義的一個文件夾)。這個文件可以包含一個或多個HTML的<OBJECT>元素,用于創建將在該應用程序或用戶會話內使用的組件實例。
下面的代碼是global.asa文件的一個例子。我們只關注<OBJECT>元素以及以Set關鍵字開始的那些代碼行:
<!-- Declare instance of the ASPCounter component with application-level scope //--> <OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Application” PROGID=”MSWC.Counters”> </OBJECT> <!-- Declare instance of the ASPContentLimk component with session-level scope //--> <OBJECT ID=”ASPContentLink” RUNAT=”Server” SCOPE=”Session” PROGID=”MSWC.NextLink”> </OBJECT> <SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”> Sub Application_onStart() ‘Create an instance of an ADO Recordset 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 the Application Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string Application(“Visit_Count”) = 0 ‘Set Counter variable to zero End Sub Sub Application_onEnd() Set Application(“ADOConnection”) = Nothing End Sub Sub Sesson_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 arry 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 intVisits = Application(“Visit_Count”) +1 Application(“Visit_Count”) = intVisits Application.Unlock End Sub Sub Session_onEnd() Set Session(“ASPAdRotator”) = Nothing End Sub </SCRIPT> |
Application(“variable_name”) = variable_value Application(“variable_name”) = variant_array_variable_name Set Application(“variable_name”) = object_reference |
variable_value = Application(“variable_name”) variant_array_variable = Application(“variable_name”) Set object_reference = Application(“variable_name”) |
新聞熱點
疑難解答