ASP Global.asa



Global.asa file

The Global.asa file is an optional file that contains declarations of objects, variables, and methods that are accessed by every page in the ASP application. .

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

Global.asa file can only contain the following content:

  • Application event

  • Session event

  • <object> Declaration

  • TypeLibrary Declaration

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


Events in Global.asa

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

Application_OnStart - This event occurs when the first user calls the first page of the 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 whenever 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 the user does not request any page within the specified time (the default time is 20 minutes), the user 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.

A Global.asa file might look like this:

<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 cannot insert scripts using ASP's script delimiters (<% and %>) in the Global.asa file, we need to place the subroutine in the HTML's < Inside the script> element.


<object> Statement

Objects with session or application scope can be created in the Global.asa file by using the <object> tag.

Note: <object> tag should be located outside the <script> tag!

Syntax

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

ParameterDescription
scopeSet the scope of the object (Session or Application) .
idSpecify a unique id for the object.
ProgIDThe id associated with ClassID. The format of ProgID is: [Vendor.]Component[.Version].

ProgID or ClassID must be specified.

ClassIDSpecifies a unique id for the COM class object.

ProgID or ClassID must be specified.

Example

The first example creates a session scope object named "MyAd" by using the ProgID parameter:

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

The second instance creates an application scope object named "MyConnection" by using the ClassID parameter:

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

Objects declared in the 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 the "MyAd" object from any page in an ASP application:

A .ASP file :

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


TypeLibrary Statement

A TypeLibrary is a container that contains DLL files that correspond to COM objects. By including a call to TypeLibrary in the Global.asa file, you can access the COM object's constants and your ASP code can better report errors. If your Web application relies on COM objects of data types that have been declared in a type library, you can declare the type library in Global.asa.

Syntax

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

##ParameterDescriptionfileSpecifies the absolute path to the type library. uuid specifies the unique identifier of the type library. versionOptional. Used to select version. If the required version is not found, the closest version will be used. lcidOptional. The locale identifier used for the type library.

Error value

The server will return one of the following error messages:

file parameter or uuid parameter, both are indispensable.

file parameter or uuid parameter, both are indispensable.

Error codeDescription
ASP 0222Invalid type library specification
ASP 0223Type library not found
ASP 0224Unable to load type library
ASP 0225Unable to wrap type library

Note: The METADATA tag can appear anywhere in the Global.asa file (either inside or outside the <script> tag). However, we still recommend placing the METADATA tag at the top of the Global.asa file.


Limitations

Limitations on what can be referenced in the Global.asa file:

  • You cannot display content in the Global.asa file text. No information can be displayed on this file.

  • You can use Server and Application objects only in the 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 is often used to initialize variables.

The following example demonstrates how to detect the exact time when a visitor first arrives at a Web site. The time is stored in a Session object named "started", and the value of the "started" variable can be accessed by any ASP page in the application:

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

Global.asa is also available To control page access.

The following example demonstrates how to redirect each new visitor to another page, in this case a page named "newpage.asp":

<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, the Application_OnStart subroutine also starts. The Application_OnStart subroutine then calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a recordset from the "customers" table. This record set is assigned to an array, and any ASP page can access this array without querying the database:

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

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

  • When the server starts, Application_OnStart sets the value of the Application variable "visitors" to 0.

  • Whenever a new visitor comes, the Session_OnStart subroutine will add 1 to the variable "visitors".

  • Whenever the Session_OnEnd subroutine is triggered, the subroutine decrements the variable "visitors" by 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, display the number of current visitors:

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