ASP Global.asa file
Global.asa file
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 the ASP application.
All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used in Global.asa.
The Global.asa file can only contain the following:
- Application events
- Session events
- <object> declarations
- TypeLibrary declarations
- #include directives
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 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. The code that accomplishes this task is placed in event handlers. The Global.asa file can contain four types of events:
Application_OnStart- This event occurs when the first user calls the first page of 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 whenever a new user requests his or her first page in the ASP application.
Session_OnEnd- This event occurs whenever a user ends a session. If the user does not request any pages within a specified time (the default time is 20 minutes), the user's session will end.
Application_OnEnd- This event occurs after the last user ends his or her session. Typically, this event occurs when the Web server is stopped. This subroutine is used to clean up settings after the application stops, such as deleting records or writing information to a text file.
A Global.asa file may look like this:
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:Because we cannot use ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we need to place the subroutines inside the HTML <script> element.
<object> Declaration
Objects with session or application scope can be created in the Global.asa file by using the <object> tag.
Note:The <object> tag should be placed outside the <script> tag!
Syntax
....
</object>
| Parameters | Description |
|---|---|
| scope | Sets the scope of the object (Session or Application). |
| id | Specifies a unique id for the object. |
| ProgID | The id associated with ClassID. The format of ProgID is: [Vendor.]Component[.Version]. Either ProgID or ClassID must be specified. |
| ClassID | Specifies a unique id for a COM class object. Either ProgID or ClassID must be specified. |
Examples
The first example creates a session-scoped object named "MyAd" using the ProgID parameter:
</object>
The second example creates an application-scoped object named "MyConnection" using the ClassID parameter:
classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21">
</object>
Objects declared in the Global.asa file can be used by any script in the application:
<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 certain .ASP file:
<%=MyAd.GetAdvertisement("/banners/adrot.txt")%>
TypeLibrary Declaration
A TypeLibrary is a container that holds DLL files corresponding to COM objects. By including a call to the TypeLibrary in the Global.asa file, you can access the constants of the COM object, and ASP code can also report errors better. If your Web application relies on COM objects whose data types are declared in a type library, you can declare the type library in Global.asa.
Syntax
file="filename" uuid="id" version="number" lcid="localeid"
-->
| Parameters | Description |
|---|---|
| file | Specifies the absolute path to the type library. The file parameter or the uuid parameter is required; one of the two must be present. |
| uuid | Specifies the unique identifier of the type library. The file parameter or the uuid parameter is required; one of the two must be present. |
| version | Optional. Used to select a version. If the required version is not found, the closest version will be used. |
| lcid | Optional. Used for the locale identifier of the type library. |
Error Values
The server will return one of the following error messages:
| Error Code | Description |
|---|---|
| ASP 0222 | Invalid type library specification |
| ASP 0223 | Type library not found |
| ASP 0224 | Unable to load type library |
| ASP 0225 | Unable to wrap type library |
Notes:The METADATA tag can appear anywhere in the Global.asa file (either inside or outside the <script> tags). However, we still recommend placing the METADATA tag at the top of the Global.asa file.
Restrictions
Limitations regarding what can be referenced in the Global.asa file:
- You cannot display text from the Global.asa file. This file cannot display information.
- You can only use the Server and Application objects in the Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use the Server, Application, and Session objects. In the Session_OnStart subroutine, you can use any built-in objects.
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 the 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:
sub Session_OnStart
Session("started")=now()
end sub
</script>
Global.asa can also be used to control page access.
The following example demonstrates how to redirect every new visitor to another page; in this example, they are redirected to a page named "newpage.asp":
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. Then, the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a recordset from the "customers" table. This recordset is assigned to an array, and any ASP page can access this array without querying the database:
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 counts 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 arrives, the Session_OnStart subroutine increments the variable "visitors" by 1.
- Whenever the Session_OnEnd subroutine is triggered, it decrements the variable "visitors" by 1.
Global.asa file:
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 an ASP file, display the current number of visitors:
<html>
<head>
</head>
<body>
<p>There are <%response.write(Application("visitors"))%> online now!</p>
</body>
</html>
Other Extensions