Session 对象
|
源代码网整理以下可以使用 Session 对象存储特定用户会话所需的信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。 源代码网整理以下当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。 源代码网整理以下Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在 Session 对象中。有关使用 Session 对象的详细信息,请参阅“ASP 应用程序”部分的“管理会话”。 源代码网整理以下注意 会话状态仅在支持 cookie 的浏览器中保留。 语法Session.collection|property|method 集合
源代码网整理以下 属性
源代码网整理以下 方法
源代码网整理以下 事件源代码网整理以下global.asa 文件中声明下列事件的脚本。
源代码网整理以下 源代码网整理以下有关以上事件及 global.asa 文件的详细信息, 请参阅 Global.asa 参考. 注释源代码网整理以下您可以在 Session 对象中存储值。存储在 Session 对象中的信息在会话及会话作用域内有效。下列脚本演示两种类型的变量的存储方式。 <%
Session("username") = "Janine"
Session("age") = 24
%>
源代码网整理以下但是,如果您将对象存储在 Session对象中,而且您使用 VBScript 作为主脚本语言。则必须使用关键字 Set。如下列脚本所示。 <% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %>
源代码网整理以下然后,您就可以在后面的 Web 页上调用 <% 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 组件”中的“选择线程模型”。 源代码网整理以下若您将一个数组存储在 Session对象中,请不要直接更改存储在数组中的元素。例如,下列的脚本无法运行。 <% Session("StoredArray")(3) = "new value" %>
源代码网整理以下这是因为 Session对象是作为集合被实现的。数组元素 源代码网整理以下我们极力建议您在将数组存储在 Session对象中时,在检索或改变数组中的对象前获取数组的一个副本。在对数组操作时,您应再将数组全部存储在 Session 对象中,这样您所做的任何改动将被存储下来。下列的脚本对此进行演示。 ---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
%>
示例源代码网整理以下下列代码将字符串 Session("name") = "MyName"
Session("year") = 96
Set Session("myObj") = Server.CreateObject("someObj")
%>
源代码网供稿. |
