<% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %>
然后,您就可以在后面的 Web 頁上調用 MyComponent.class1 揭示的方法和屬性,其調用方法如下:
<% Session("Obj1").MyMethod %>
也可以通過展開該對象的本地副本并使用下列腳本來調用:
<% Set MyLocalObj1 = Session("Obj1") MyLocalObj1.MyObjMethod %>
創建有會話作用域的對象的另一種方法是在 global.asa 文件中使用 <OBJECT> 標記。
但是不能在 Session 對象中存儲內建對象。例如,下面每一行都將返回錯誤。
<% Set Session("var1") = Session Set Session("var2") = Request Set Session("var3") = Response Set Session("var4") = Server Set Session("var5") = application %>
在將對象存儲到 Session 對象之前,必須了解它使用的是哪一種線程模型。只有那些標記為“Both”的對象才能存儲在沒有鎖定單線程會話的 Session 對象中。詳細信息, 請參閱“創建 ASP 組件”中的“選擇線程模型”。
---file1.asp--- <% 'Creating and initializing the array Dim MyArray() Redim MyArray(5) MyArray(0) = "hello" MyArray(1) = "some other string"
'Storing the array in the Session object Session("StoredArray") = MyArray
Response.Redirect("file2.asp") %>
---file2.asp--- <% 'Retrieving the array from the Session Object 'and modifying its second element LocalArray = Session("StoredArray") LocalArray(1) = " there"
'printing out the string "hello there" Response.Write(LocalArray(0)&LocalArray(1))
'Re-storing the array in the Session object 'This overwrites the values in StoredArray with the new values Session("StoredArray") = LocalArray %>
示例 下列代碼將字符串 MyName 分配給名為 name 的會話變量,并給名為 year 的會話變量指定一個值,而且為 some.Obj 組件的實例指定一個名為 myObj 的變量。