asp入门教程_asp入门教程:ASP applocation 全局变量与 Global.asa 文件

时间:2015-08-09  来源:ASP入门  阅读:

asp入门教程:ASP 全局变量 Global.asa 文件

Global.asa文件是一个可选文件,可以包含申报的对象,变量和方法,可以通过在每一页的ASP应用程序。


-------------------------------------------------- ------------------------------

Global.asa文件
Global.asa文件是一个可选文件,可以包含申报的对象,变量和方法,可以通过在每一页的ASP应用程序。所有有效的浏览器的脚本( JavaScript中,脚本语言, PerlScript等) ,可用于在Global.asa中。

Global.asa文件只能包含以下内容:

应用事件
会议活动
声明
TypeLibrary声明
在#包括指令
注: Global.asa文件必须存放在根目录中的ASP应用程序,每个应用程序只能有一个Global.asa文件。


-------------------------------------------------- ------------------------------

Global.asa中的活动
Global.asa中你可以告诉应用和会议的物体时该怎么做的应用/会议开始和该怎么做时,应用/会议的目的。该代码,这是放置在事件处理程序。 Global.asa文件可以包含四种类型的活动:

Application_OnStart -这一事件发生时,第一个用户要求的第一页从A SP应用程序。此事件发生后, Web服务器重新启动或之后的Global.asa文件编辑。该“ Session_OnStart ”事件发生后立即这一事件。
Session_OnStart -此事件发生每当一个新的用户要求他或她的第一页中的A SP应用程序。

Session_OnEnd -此事件发生每当用户结束了会议。用户一次会议结束后,页面已经没有要求用户指定的时间(默认情况下,这是20分钟) 。

Application_OnEnd -此事件发生后的最后一个用户已经结束了会议。通常情况下,此事件发生时, Web服务器停止。这个程序是用来设置清理后停止的应用,如删除记录或写入资料,文本文件。

阿Global.asa文件可能看起来就像这样:

 

<script language="vbscript" runat="server">
sub Application_OnStart
  "some code
end sub
sub Application_OnEnd
  "some code
end sub
sub Session_OnStart
  "some code
end sub
sub Session_OnEnd
  "some code
end sub
</script>
 

注意:由于我们不能使用ASP脚本分隔“ ( < %和% ” )插入脚本在Global.asa文件中,我们把子程序内的HTML <script>因素。


-------------------------------------------------- ------------------------------

声明
这是有可能创造物体的会议或适用范围Global.asa中使用标记。

注: 标记应以外的<script>标记!

语法

 

object runat="server" scope="scope" id="id" {progid="progID"|classid="classID"}> ....

 

ParameterDescriptionscopeSets the scope of the object (either Session or Application)idSpecifies a unique id for the objectProgIDAn id associated with a class id. The format for ProgID is [Vendor.]Component[.Version]

Either ProgID or ClassID must be specified.

ClassIDSpecifies a unique id for a COM class object.

Either ProgID or ClassID must be specified

 

范例
第一个例子创建了一个对象范围的会议名为“ MyAd ”使用的ProgID参数:

 


 

第二个例子创建了一个对象的适用范围命名为“ MyConnection ”使用的ClassID参数:

 


 

申报的对象在Global.asa文件可以使用任何脚本的应用程序:

 

GLOBAL.ASA:


You could reference the object "MyAd" from any page in the ASP application:

SOME .ASP FILE:

<%=MyAd.GetAdvertisement("/banners/adrot.txt")%> 
 

TypeLibrary声明
阿TypeLibrary是一个容器的内容DLL文件相应的COM对象。包括呼吁TypeLibrary在Global.asa文件中,常量的COM对象可以访问,错误可以得到更好的报告的ASP代码。如果您的Web应用程序依赖于COM对象已宣布的数据类型的类型库,你可以宣布类型库Global.asa中。

语法

 

 

Parameter Description file Specifies an absolute path to a type library.

Either the file parameter or the uuid parameter is required

uuid Specifies a unique identifier for the type library.

Either the file parameter or the uuid parameter is required

version Optional. Used for selecting version. If the requested version is not found, then the most recent version is used lcid Optional. The locale identifier to be used for the type library

 

误差值
该服务器可返回下列其中一个错误讯息:

 

Error Code Description ASP 0222 Invalid type library specification ASP 0223 Type library not found ASP 0224 Type library cannot be loaded ASP 0225 Type library cannot be wrapped

 

注:元数据标签可以出现在Global.asa文件(包括内部和外部<script>标记) 。然而,建议元标记显示上方附近的Global.asa文件。


-------------------------------------------------- ------------------------------

限制
限制什么,您可以在Global.asa文件:

你可以不显示文字,写在Global.asa文件。此文件无法显示的信息
您只能使用服务器和应用中的对象Application_OnStart和Application_OnEnd子程序。在Session_OnEnd副程式,您可以使用服务器,应用,以及会议的对象。在Session_OnStart副程式您可以使用任何内置对象

-------------------------------------------------- ------------------------------

如何使用子程序
Global.asa中是经常被用来初始化变量。

下面的例子显示了如何检测的准确时间访问者第一次来到网站上。时间是储存在一个会话变量名为“开始” ,以及价值的“开始”变量可以从任何ASP网页中的应用:

 

<script language="vbscript" runat="server">
sub Session_OnStart
Session("started")=now()
end sub
</script>
 

Global.asa中也可以被用来控制页面访问。

下面的例子显示了如何重新每一个新的访问者到另一个网页,在这种情况下,在网页上所谓的“ newpage.asp ” :

 

<script language="vbscript" runat="server">
sub Session_OnStart
Response.Redirect("newpage.asp")
end sub
</script>
 

您可以包括职能的Global.asa文件。

在下面的例子中的Application_OnStart副程式发生时, Web服务器启动。

然后Application_OnStart副程式呼叫另一个副程式名为“ getcustomers ” 。该“ getcustomers ”

子程序打开一个数据库和检索纪录的“客户”表。该纪录是分配给一个数组,它可以从任何ASP网页查询资料库:

<script language="vbscript" runat="server">
sub Application_OnStart
getcustomers
end sub
sub getcustomers 
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=conn.execute("select name from customers")
Application("customers")=rs.GetRows
rs.Close
conn.Close
end sub
</script>
 

Global.asa中为例
在这个例子中,我们将创建一个Global.asa文件算数的数量目前的访客。

Application_OnStart集的应用程序变量“游客”为0时,服务器启动
副程式的Session_OnStart了一个变数“游客”每当一个新的访问者到达
副程式的Session_OnEnd减去一个“访客”每次这种子程序触发
Global.asa文件:

 

<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub
</script>
 

要显示当前的一些游客在ASP文件:

 





There are <%response.write(Application("visitors"))%> online now!

转载请注明来自www.111cn.net/asp/asp.html
 

asp入门教程_asp入门教程:ASP applocation 全局变量与 Global.asa 文件

http://m.bbyears.com/aspjiaocheng/17175.html

推荐访问: