网络编程
位置:首页>> 网络编程>> Asp编程>> asp详解session的用法(2)

asp详解session的用法(2)

 来源:月光软件站 发布时间:2007-09-07 10:22:00 

标签:session,用法
也可以通过展开该对象的本地副本并使用下列脚本来调用:

<% 
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对象是作为集合被实现的。数组元素 StoredArray(3) 未获得新的赋值。而此值将包含在 Application 对象集合中,并将覆盖此位置以前存储的任何信息。
我们极力建议您在将数组存储在 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 
%> 


示例
下列代码将字符串 MyName 分配给名为 name 的会话变量,并给名为 year 的会话变量指定一个值,而且为 some.Obj 组件的实例指定一个名为 myObj 的变量。

Session("name") = "MyName" 
Session("year") = 96 
Set Session("myObj") = Server.CreateObject("someObj") 
%> 



0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com