Latest web development tutorials

ASP Global.asa

Global.asa file

Global.asa file is an optional file that can contain declarations are subject ASP applications every page accessed, variables and methods.

All legitimate browser scripts (JavaScript, VBScript, JScript, PerlScript etc.) can be used in the Global.asa.

Global.asa file can contain only the following:

  • Application Events
  • Session Event
  • <Object> statement
  • TypeLibrary Statement
  • #include directive

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


Global.asa events

In Global.asa you can tell the application and session objects when the application / session beginning what to do, what to do when the end of the application / session. The code to accomplish this task is placed in the event handler. Global.asa file can contain four types of events:

Application_OnStart - This event occurs when a user calls the first ASP application first page.This event will restart the Web server or occurring after the Global.asa file is edited. "Session_OnStart" event occurs after this event occurs immediately.

Session_OnStart - This event occurs whenever a new user requests him (her) in an ASP application in the first page.

Session_OnEnd - This event occurs whenever the end user session.If the user does not request any page, the user session will end within a predetermined time (the default time is 20 minutes).

Application_OnEnd - This event after the user ends their session last occurs.Typically, the event occurs when the Web server is stopped. This subroutine is used after the application to stop purge settings, such as delete records or write information to a text file.

A Global.asa file might look like:

<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>

Note: Since we can not use the ASP script delimiters (<% and%>) insert scripts in the Global.asa file, we need to handle routine placed in the HTML <script> element inside.


<Object> statement

Can <object> tag to create objects with session or application scope in Global.asa file by using.

NOTE: <object> tag should be in the <script> tag outside!

grammar

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

参数 描述
scope 设置对象(Session 或 Application)的作用域。
id 为对象指定一个唯一的 id。
ProgID 与 ClassID 关联的 id。ProgID 的格式是:[Vendor.]Component[.Version]。

ProgID 或 ClassID 必需被指定。

ClassID 为 COM 类对象指定一个唯一的 id。

ProgID 或 ClassID 必需被指定。

Examples

The first instance by using the ProgID parameter to create a session scope object called "MyAd" of:

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

The second instance is created by using the ClassID parameter application scope objects called "MyConnection" of:

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

Objects declared in the Global.asa file can be any application script:

GLOBAL.ASA:

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

您可以从 ASP 应用程序中的任意页面引用 "MyAd" 对象:

某个 .ASP 文件:

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


TypeLibrary Statement

TypeLibrary (type library) is a container comprising a corresponding COM object DLL files. By including a call to TypeLibrary in the Global.asa file, you can access the constants of the COM object, and the ASP code also better able to report errors. If your Web application relies on COM object data types declared in a type library, you can type libraries in Global.asa statement.

grammar

<!--METADATA TYPE="TypeLib"
file="filename" uuid="id" version="number" lcid="localeid"
-->

参数 描述
file 规定指向类型库的绝对路径。

file 参数或者 uuid 参数,两者缺一不可。

uuid 规定了类型库的唯一的标识符。

file 参数或者 uuid 参数,两者缺一不可。

version 可选。用于选择版本。如果没有找到需要的版本,将使用最接近的版本。
lcid 可选。用于类型库的地区标识符。

Error value

The server returns one of the following error message:

错误代码 描述
ASP 0222 无效的类型库规范
ASP 0223 没有找到类型库
ASP 0224 无法加载类型库
ASP 0225 无法包装类型库

Note: METADATA tag in the Global.asa file anywhere (inside and outside the <script> tag can be) appears.However, we still recommend METADATA tag placed at the top of the Global.asa file.


limited

About Global.asa file can be referenced in the content defined:

  • Global.asa file that you can not display text. This file can not display information.
  • You can only use Server and Application objects Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use Server, Application and Session objects. In the Session_OnStart subroutine, you can use any built-in object.

How to use subroutines

Global.asa commonly used to initialize variables.

The following example demonstrates how to detect the exact time of Web site visitors first arrive. Time is stored in the Session object named "started" in, and the value of "started" variable can be any ASP application page views:

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

Global.asa can also be used to control access to the page.

The following example shows how to redirect every new visitor to another page, in this case will be directed to a "newpage.asp" page named:

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

You can include functions in the Global.asa file.

In the following example, when the Web server starts, Application_OnStart subroutine will start. Then, Application_OnStart calls another subroutine named "getcustomers" subroutine. "Getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. This record is assigned to an array of assembly, without querying the database, any ASP pages to access this array:

<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 examples

In this example, we will create a calculation of the current number of visitors Global.asa file.

  • When the server starts, Application_OnStart set the Application variable "visitors" is 0.
  • Whenever a new visitors come to visit, Session_OnStart subroutine will be to the variable "visitors" plus one.
  • Whenever Session_OnEnd subroutine is triggered, the subroutine will subtract from the variable "visitors" 1.

Global.asa file:

<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>

In the ASP file that displays the current number of visitors:

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