您的位置:首页 > 移动开发

ASP内置对象之Application对象&Server对象&Session对象

2008-05-05 21:05 441 查看
一、Server对象

(一)属性
ScriptTimeout:脚本在被终止前可运行的最大秒数,单位为秒,默认值为90秒

例:
----------------------------------------test.asp--------------------------------------------------------------------------------
<% @ language="JavaScript" %>
<html>
<head>
  <title>Server对象的ScriptTimeOut属性</title>
</head>
<body>
  <%
    Response.Write("默认情况下,Server对象的ScriptTimeOut值为:"+Server.ScriptTimeOut+"<br>");
    Server.ScriptTimeOut=200;
    Response.Write("修改后,Server对象的ScriptTimeOut值为:"+Server.ScriptTimeOut+"<br>");
  %>
</body>
</html>
----------------------------------------test.asp--------------------------------------------------------------------------------

(二)方法
1.HTMLEncode方法
(1)功能:对一段指定的字符串应用 HTML 编码。
(2)格式:Server.HTMLEncode(string)

如:
-----------------------------------------HTMLEncode.asp---------------------------------------------------------------

----
<% @ language="JavaScript" %>
<html>
<head>
  <title>Server对象的HTMLEncode方法</title>
</head>
<body>
  <%
    var str=Server.HTMLEncode("<a href=/"http://blog.csdn.net/zhangyang0402/">张扬的博客

</a>");
    Response.Write("HTML中超链接举例:"+"<br>"+str+"<br>");
  %>
  <a href="http://blog.csdn.net/zhangyang0402">张扬的博客</a><br>
</body>
</html>
-----------------------------------------HTMLEncode.asp-------------------------------------------------------------

2.MapPath方法
(1)功能:返回指定文件在服务器上的物理路径
(2)格式:Server.MapPath(path)

如:
-------------------------------------MapPath.asp--------------------------------------------------------------------------
<% @ language="JavaScript" %>
<html>
<head>
  <title>Server对象的MapPath方法</title>
</head>
<body>
  <%
    Response.Write("当前文件MapPath.asp的路径为:"+Server.MapPath("MapPath.asp")+"<br>");
  %>
</body>
</html>
-------------------------------------MapPath.asp----------------------------------------------------------------------------
运行后,输出结果为:
当前文件MapPath.asp的路径为:c:/inetpub/wwwroot/asp/server/mappath.asp

3.CreateObject方法★★★★★
(1)功能:创建服务器组件的实例
通过这些组件,我们可以实现数据库连接、文件访问等重要功能
(2)格式:Server.CreateObject( progID )
注:
a.默认情况下,由 Server.CreateObject 方法创建的对象具有页作用域。这就是说,再当前 ASP 页处理

完成之后,服务器将自动破坏这些对象。如果要创建有会话或应用程序作用域的对象,可以使用 <

OBJECT>标记并设置 SESSION 或 APPLICATION 的 SCOPE 属性,也可以在对话及应用程序变量中存储该对

象。如下例程 :< % Set Session("ad") = Server.CreateObject("MSWC.AdRotator")%>

b.不能创建与内建对象同名的对象实例,否则,如下列脚本将返回错误。
<% Set Response = Server.CreateObject("Response") %>

例:
var con=Server.CreateObject("ADODB.Connection"),
    rs =Server.CreateObject("ADODB.Recordset");

二、Application对象

Application对象可用来在给定应用程序的多用户之间共享信息,一个基于ASP的应用程序定义为在虚拟目

录及其子目录下的所有.asp文件
(一)集合
1.Contents
(1)功能:Contents集合是通过脚本命令添加到应用程序的一组选项,可以使用Contents集合来获得添加到

应用程序范围内的列表选项

(2)格式:Application.Contents(Key)
Key:指定要提取的选项的名字

注:
1.Application对象没有内置属性,但我们可以自己定义它的属性
----------------------------------test.asp--------------------------------------------------------------
<html>
<head>
  <title>测试Application对象</title>
</head>
<body>
  <%
      Application("info1")="五一大优惠"
      Application("info2")="支持奥运,反对藏独!"
  %>
  <a href="second.asp">测试Application对象在多用户间共享信息</a>
</body>
</html>
----------------------------------test.asp------------------------------------------------------------

----------------------------------second.asp-------------------------------------------------------
<html>
<head>
  <title>测试结果显示</title>
</head>
<body>
  <%
      dim info1,info2
      info1=Application("info1")
      info2=Application("info2")
      Response.Write("第一条信息是:"&info1&"<br>")
      Response.Write("第二条信息是:"&info2&"<br>")
  %>
</body>
</html>
----------------------------------second.asp:-------------------------------------------------------

事实上,自己定义的Application对象的属性就是在Contents集合中,这样我们也可以获得各项的值
--------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
  <title>测试结果显示</title>
</head>
<body>
  <%
      dim info3,info4,item,index
      info3=Application.Contents("info1")
      info4=Application.Contents("info2")
      Response.Write("第一条信息是:"&info3&"<br>")
      Response.Write("第二条信息是:"&info4&"<br>")
      '使用for each ...next循环输出Contents集合元素
      For Each item in Application.Contents
         Response.Write(item&"="&Application.Contents(item)&"<br>")
      Next
      '使用For循环输出Application对象Contents集合
      For index=1 to Application.Contents.Count
         Response.Write("Application.Contents("&index&")="&Application.Contents(index)

&"<br>")
      Next
  %>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------
Application.Contents集合包含了在应用程序中不使用<OBJECT>标签的情况下所声明的选项,包括通过

Server.CreateObject创建的对象和通过Application声明建立的标量变量

2.StaticObjects
StaticObjects集合包含所有使用HTML的<OBJECT>标签追加到Application的对象

(二)方法

1.Lock :

使用Lock方法可防止其他客户修改保存在Application对象的变量,确保每次只有一个客户访问和修改

Application对象中的变量.
格式:Application.Lock

2.Unlock

当Application对象被锁定后,可使用Unlock方法解锁,确保其他客户也能修改保存在Application对象中的

变量
格式:Application.Unlock
如:
-------------------------------------------------------------------------------------------------------------------------
  <%
      dim counter
      Application.Lock    '锁定
      counter=Application.Contents("visit_counter")
      Application.Contents("visit_counter")=counter+1
      Application.Unlock  '解锁
      Response.Write("本页面已访问<b>"&Application.Contents("visit_counter")&"</b>次!       

              <br>")
  %>
------------------------------------------------------------------------------------------------------------------------

三、Session对象

Session对象用于保存每个用户的信息。当某个没有Session的用户访问应用程序的一个Web页面时,Web服

务会自动地为该用户创建一个Session。

区别Application对象和Session对象
Application对象:访问页面的不同用户间共享信息
Session对象:不能在两个用户间共享信息,每个用户都有自己的Session

(一)集合
1.Contents
(1)功能:在不使用<OBJECT>标签的情况下,Session.Contents集合包括了所有为一个Session创建的项。

这一集合可为指定的Session某个项赋值,或通过循环提取出Session里的所有项的值

(2)格式:Session.Contents( Key )
Key:要提取的属性名称
如:
--------------------------------- save.asp----------------------------------------------------
   <%
      '将用户信息写入Session保存
      Session.Contents("username")=Request.Form("username") 
   %>
----------------------------------save.asp----------------------------------------------------

----------------------------------test.asp------------------------------------------------------

   <%
      dim username
      '从Session中读取用户信息
      username=Session.Contents("username")
      Response.Write("欢迎你,"&username+"!<br><测试成功!<br>")
   %>
----------------------------------test.asp-----------------------------------------------------
2.StaticObjects
StaticObjects集合包括了所有在Session对象范围内的由<OBJECT>标签创建的对象.

(二)属性
1.SessionID

SessionID属性返回用户的Session标识。每一个Session都有一个该Session被创建时Web服务器产生的唯

一标识符

格式:Sessino.SessionID

2.Timeout
Timeout属性指定了Session对象的时限,单位是分钟。如果用户在Timeout规定的时间内没有刷新或请求

页面,则Session将会终止。默认情况下,属性值为20分钟

(三)方法
Abandon:
Abandon方法会破坏保存在Session对象中的所有对象并释放它们的资源。如果不显示调用Abandon方法,

服务器会在Session超时的时候销毁它。
格式:Session.Abandon

注:当调用Abandon方法时,当前的Session对象进入删除队列,但是实际上并未删除,直到当前页面所有

的的脚本命令都被处理完后,才真正删除当前Session对象
如:
-----------------------------------abandon.asp-----------------------------------------------------
<html>
<head>
  <title>Session对象的方法</title>
</head>
<body>
   <%
       Session.Abandon  '销毁当前Session
       Session.Contents("username")="zhangyang" 
       Response.Write("你的SessionID:"&Session.SessionID&"<br>")
       Response.Write("你的用户名:"&Session.Contents("username")&"<br>")
   %>
   <a href="next.asp">下一个页面</a>
</body>
</html>
------------------------------------abandon.asp---------------------------------------------------

-------------------------------------next.asp--------------------------------------------------------
<html>
<head>
  <title>Session对象的方法</title>
</head>
<body>
   <%
       dim username,id
       id=Session.SessionID
       username=Session.Contents("username")
       Response.Write("你的SessionID:"&Session.SessionID&"<br>")
       Response.Write("你的用户名:"&Session.Contents("username")&"<br>")
   %>
</body>
</html>
-------------------------------------next.asp---------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息