Home >Backend Development >C#.Net Tutorial >Detailed introduction to the Global.asa file in ASP

Detailed introduction to the Global.asa file in ASP

Y2J
Y2JOriginal
2017-05-19 10:55:301503browse

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.

Global.asa file

The Global.asa file is an optional file that contains information that can be accessed by every page in an ASP application. Declaration of objects, variables, and methods. All legal browser scripts can be used in Global.asa.

The Global.asa file can contain the following:

Application event

Session event

273238ce9338fbb04bee6997e5552b95 Statement

TypeLibrary declaration

#include directive

Note: The Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa document.

Events in Global.asa

In Global.asa, we can tell the application and session objects what to do when starting and ending. The code to accomplish this task is placed in the event operator. The Global.asa file can contain four types of events:

Application_OnStart - This event occurs when the first user calls the first page from an ASP application. This event occurs after the web server is restarted or the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.

Session_OnStart - This event occurs every time a new user requests his or her first page in an ASP application.

Session_OnEnd - This event occurs whenever the user ends the session. If no page is requested within the specified time (the default event is 20 minutes), the session will end.

Application_OnEnd - This event occurs after the last user ends their session. Typically, this event occurs when the web server is stopped. This subroutine is used to clear settings after the application is stopped, such as deleting records or writing information to a text file.

Global.asa file may look like this:

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

Note: Since ASP's script delimiters (1c57a0bf76ccc0c84c2b546a40c0f101) cannot be used in Global.asa To insert a script into a file, we need to use the HTML 3f1c4e4b6b16bbbd69b2ee476dc4f83a element.

273238ce9338fbb04bee6997e5552b95 Statement

Objects with session or application scope can be created in the Global.asa file by using the 273238ce9338fbb04bee6997e5552b95 tag.

Note: The 273238ce9338fbb04bee6997e5552b95 tag should be outside the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag.

Syntax:

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

Parameters                                                                                                                                                                                                .

id Specify a unique id for the object.

ProgID The id associated with ClassID. The format of ProgID is: [Vendor.]Component[.Version]. ProgID or ClassID must be specified.

ClassID Specifies a unique id for the COM class object. ProgID or ClassID must be specified.

Examples

The first instance creates a session scope object named "MyAd" and uses the ProgID parameter:

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

The second An instance is created named "MyConnection" using the ClassID parameter

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

These objects declared in this Global.asa file can be used by any script in the application.

GLOBAL.ASA:

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

You can reference this "MyAd" object from any page in your ASP application:

A .ASP file:

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

TypeLibrary Declaration

TypeLibrary (type library) is a container that holds DLL files corresponding to COM objects. By including a call to TypeLibrary in Global.asa, you can access the COM object's constants and your ASP code can better report errors. If your site's applications depend on COM objects whose data types have been declared in a type library, you can declare the type library in Global.asa.

Syntax:

<!--METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->

Parameters                                                                                                                                                                                                                       Syntax: Parameter file or uuid, both are indispensable.

uuid Specifies the unique identifier for the type library. Parameter file or uuid, both are indispensable.

version Optional. Used to select version. If the specified version is not found, the closest version will be used. ​

lcid ​ Optional. The locale identifier used for the type library.

Error value

The server will return one of the following error messages:

错误        代码        描述

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    

注释:METADATA 标签可位于 Global.asa 文件中的任何位置(在 3f1c4e4b6b16bbbd69b2ee476dc4f83a 标签的内外均可)。不过,我们还是推荐将 METADATA 标签放置于 Global.asa 文件的顶部。

限定

关于可以在 Global.asa 文件中引用的内容的限定:

你无法显示 Global.asa 文件中的文本。此文件无法显示信息。

你只能在 Application_OnStart 和 Application_OnEnd 子例程中使用 Server 和 Application 对象。在 Session_OnEnd 子例程中,你可以使用 Server、Application 和 Session 对象。在 Session_OnStart 子例程中,你可使用任何内建的对象。

如何使用子例程

Global.asa 常用于对变量进行初始化。

下面的例子展示如何检测访问者首次到达站点的确切时间。时间存储在名为 "started" 的 Session 对象中,并且 "started" 变量的值可被应用程序中的任何 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 中包含函数。

在下面的例子中,当 Web 服务器启动时,Application_OnStart 子例程也会启动。随后,Application_OnStart 子例程会调用另一个名为 "getcustomers" 的子例程。"getcustomers" 子例程会打开一个数据库,然后从 "customers" 表中取回一个记录集。此记录集会赋值给一个数组,在不查询数据库的情况下,任何 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 设置当服务器启动时,Application 变量 "visitors" 的值为 0。

每当有新用户访问时,Session_OnStart 子例程就会给变量 "visitors" 加 1。

每当 Session_OnEnd 子例程被触发时,此子例程就会从变量 "visitors" 减 1。

Global.asa 文件:

<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStartApplication.LockApplication("visitors")=Application("visitors")+1Application.UnLockEnd SubSub Session_OnEndApplication.LockApplication("visitors")=Application("visitors")-1Application.UnLockEnd Sub</script>
此 ASP 文件会显示当

前用户的数目:

<html>
<head>
</head>
<body>
<p>There are <%response.write(Application("visitors"))%> online now!</p>
</body>
</html>

【相关推荐】

ASP免费视频教程

The above is the detailed content of Detailed introduction to the Global.asa file in ASP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn