session 對(duì)象 可以使用 Session 對(duì)象存儲(chǔ)特定用戶會(huì)話所需的信息。這樣,當(dāng)用戶在應(yīng)用程序的 Web 頁(yè)之間跳轉(zhuǎn)時(shí),存儲(chǔ)在 Session 對(duì)象中的變量將不會(huì)丟失,而是在整個(gè)用戶會(huì)話中一直存在下去。
當(dāng)用戶請(qǐng)求來自應(yīng)用程序的 Web 頁(yè)時(shí),如果該用戶還沒有會(huì)話,則 Web 服務(wù)器將自動(dòng)創(chuàng)建一個(gè) Session 對(duì)象。當(dāng)會(huì)話過期或被放棄后,服務(wù)器將終止該會(huì)話。
<% Set Session("var1") = Session Set Session("var2") = Request Set Session("var3") = Response Set Session("var4") = Server Set Session("var5") = application %>
在將對(duì)象存儲(chǔ)到 Session 對(duì)象之前,必須了解它使用的是哪一種線程模型。只有那些標(biāo)記為“Both”的對(duì)象才能存儲(chǔ)在沒有鎖定單線程會(huì)話的 Session 對(duì)象中。詳細(xì)信息, 請(qǐng)參閱“創(chuàng)建 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 的會(huì)話變量,并給名為 year 的會(huì)話變量指定一個(gè)值,而且為 some.Obj 組件的實(shí)例指定一個(gè)名為 myObj 的變量。