因为每一个和Server 端联机的客户端都是独立的Session,所以Server 端需要额外的资源来管理这些Session。有时候使用者正在浏览网页时,突然去做其它的事情而没有把网页的联机关闭;如果Server 端一直浪费资源在管理这些Session 上,那么势必会让服务器的效率降低。所以当使用者超过一段时间没有动作时,我们就可以将Session 释放。要更改Session 对象的有效期限,只要设定TimeOut 属性即可;TimeOut 属性的默认值是20 分钟。下列范例将Session 对象的TimeOut 属性设定为一分钟:
<Html> <Form Runat="Server"> <Asp:Button Id="Button1" Text="显示" OnClick="Button1_Click" Runat="Server" /> 目前时间:<Asp:Label Id="Label1" Runat="Server" /> <P> 第一个Session 的值:<Asp:Label Id="Label2" Runat="Server" /><Br> 第二个Session 的值:<Asp:Label Id="Label3" Runat="Server" /><Br> </Form> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object,e As Eventargs) If Not Page.IsPostBack Then Session("Session1")="Value1" Session("Session2")="Value2" Session.Timeout=1 Label1.Text=Format(Now(),"HH:MM:SS") Label2.Text=Session("Session1") Label3.Text=Session("Session2") End If End Sub Sub Button1_Click(Sender As Object,e As Eventargs) Label1.Text=Format(Now(),"hh:mm:ss") Label2.Text=Session("Session1") Label3.Text=Session("Session2") End Sub </Script> </Html>
第一次进入这个网页时,Session 对象变量的值会被显示出来;接着我们不要做任何动作静待一分钟,一分钟过后按下Button1 时,Session 对象变量的内容便被释放:
|