教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp  >  正文 asp入门教程:ASP applocation 全局变量与 Global.asa 文件

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

发布时间:2016-10-08   编辑:jiaochengji.com
教程集为您提供asp入门教程:ASP applocation 全局变量与 Global.asa 文件等资源,欢迎您收藏本站,我们将为您提供最新的asp入门教程:ASP applocation 全局变量与 Global.asa 文件资源

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

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


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

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

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

应用事件
会议活动
<object>声明
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文件可能看起来就像这样:

 

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

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


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

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

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

语法

 

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

 

<table class="ex" cellspacing="0" cellpadding="3" width="100%" border="1"><tbody><tr><th valign="top" align="left" width="20%">Parameter</th><th valign="top" align="left" width="80%">Description</th></tr><tr><td valign="top">scope</td><td valign="top">Sets the scope of the object (either Session or Application)</td></tr><tr><td valign="top">id</td><td valign="top">Specifies a unique id for the object</td></tr><tr><td valign="top">ProgID</td><td valign="top">An id associated with a class id. The format for ProgID is [Vendor.]Component[.Version]

Either ProgID or ClassID must be specified.

</td></tr><tr><td valign="top">ClassID</td><td valign="top">Specifies a unique id for a COM class object.

Either ProgID or ClassID must be specified

</td></tr></tbody></table>

 

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

 

<pre><object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"> </object></pre> <pre> </pre> <pre>

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

 

<pre><object runat="server" scope="application" id="MyConnection" classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21"> </object></pre> <pre> </pre> <pre>

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

 

GLOBAL.ASA:

<pre><object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"> </object></pre>

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

SOME .ASP FILE:

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

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

语法

 

<table class="ex" cellspacing="0" cellpadding="3" width="100%" border="1"><tbody><tr valign="top"><td><pre><!--METADATA TYPE="TypeLib" file="filename" uuid="typelibraryuuid" version="versionnumber" lcid="localeid" --></pre> </td> </tr> </tbody> </table>

 

<table class="ex" cellspacing="0" cellpadding="3" width="100%" border="1"> <tbody> <tr> <th valign="top" align="left" width="20%">Parameter</th> <th valign="top" align="left" width="80%">Description</th> </tr> <tr> <td valign="top">file</td> <td valign="top">Specifies an absolute path to a type library.

Either the file parameter or the uuid parameter is required

</td> </tr> <tr> <td valign="top">uuid</td> <td valign="top">Specifies a unique identifier for the type library.

Either the file parameter or the uuid parameter is required

</td> </tr> <tr> <td valign="top">version</td> <td valign="top">Optional. Used for selecting version. If the requested version is not found, then the most recent version is used</td> </tr> <tr> <td valign="top">lcid</td> <td valign="top">Optional. The locale identifier to be used for the type library</td> </tr> </tbody> </table>

 

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

 

<table class="ex" cellspacing="0" cellpadding="3" width="100%" border="1"> <tbody> <tr> <th valign="top" align="left" width="20%">Error Code</th> <th valign="top" align="left" width="80%">Description</th> </tr> <tr> <td valign="top">ASP 0222</td> <td valign="top">Invalid type library specification</td> </tr> <tr> <td valign="top">ASP 0223</td> <td valign="top">Type library not found</td> </tr> <tr> <td valign="top">ASP 0224</td> <td valign="top">Type library cannot be loaded</td> </tr> <tr> <td valign="top">ASP 0225</td> <td valign="top">Type library cannot be wrapped</td> </tr> </tbody> </table>

 

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


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

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

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

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

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

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

 

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

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

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

 

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

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

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

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

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

<pre><script language="vbscript" runat="server"></pre> <pre>sub Application_OnStart getcustomers end sub</pre> <pre>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</pre> <pre></script></pre> <pre> </pre> <pre>

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

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

 

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

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

 

<pre><html> <head> </head> <body> <p> There are <%response.write(Application("visitors"))%> online now! </p> </body> </html></pre> <pre>转载请注明来自www.jiaochengji.com/asp/asp.html</pre> <pre> </pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>

您可能感兴趣的文章:
asp入门教程:ASP applocation 全局变量与 Global.asa 文件
Global.asa文件的使用及Chat程序
asp 在线人数统计代码
asp入门教程:ASP Request 简单介绍
php和asp语法上的区别是什么
ASP Application 对象教程
什么是Global.asa文件
asp入门教程:ASP Session 对象简介
asp文件怎么转换成php文件
ASP 3.0高级编程(十九)

[关闭]
~ ~