• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
ASP And ASP.NET Tutorials
  • ASP.NET Web Pages - Tutorial
  • ASP.NET Web Pages - Adding Razor Code
  • ASP.NET Web Pages - Page Layout
  • ASP.NET Web Pages - Folders
  • ASP.NET Web Pages - Global Pages
  • ASP.NET Web Pages - HTML Forms
  • ASP.NET Web Pages - Objects
  • ASP.NET Web Pages - Files
  • ASP.NET Web Pages - Databases
  • ASP.NET Web Pages - Helpers
  • ASP.NET Web Pages - The WebGrid Helper
  • ASP.NET Web Pages - The Chart Helper
  • ASP.NET Web Pages - The WebMail Helper
  • ASP.NET Web Pages - WebSecurity Object
  • ASP.NET Web Pages - Publishing The Website
  • ASP.NET Web Pages - Classes
  • ASP.NET Razor - Markup
  • ASP.NET Razor - C# And VB Code Syntax
  • ASP.NET Razor - C# Variables
  • ASP.NET Razor - C# Loops And Arrays
  • ASP.NET Razor - C# Logic Conditions
  • ASP.NET Razor - VB Variables
  • ASP.NET Razor - VB Loops And Arrays
  • ASP.NET Razor - VB Logic Conditions
  • ASP Tutorial
  • ASP Syntax
  • ASP Variables
  • ASP Procedures
  • VBScript Conditional Statements
  • VBScript Looping
  • ASP Forms And User Input
  • ASP Cookies
  • ASP Session Object
  • ASP Application Object
  • ASP Including Files
  • ASP The Global.asa File
  • ASP AJAX
  • ASP Sending E-mail With CDOSYS
  • VBScript Functions
  • VBScript Keywords
  • ASP Response Object
  • ASP Application Object
  • ASP Session Object
  • ASP Server Object
  • ASP ASPError Object
  • ASP FileSystemObject Object
  • ASP TextStream Object
  • ASP Drive Object
  • ASP File Object
  • ASP Folder Object
  • ASP Dictionary Object
  • ASP AdRotator Component
  • ASP Browser Capabilities Component
  • ASP Content Linking Component
  • ASP Content Rotator Component (ASP 3.0)
  • ASP Quick Reference
  • ADO Introduction
  • ADO Database Connection
  • ADO Recordset
  • ADO Display
  • ADO Queries
  • ADO Sort
  • ADO Add Records
  • ADO Update Records
  • ADO Delete Records
  • ADO Demonstration
  • ADO Speed Up With GetString()
  • ADO Command Object
  • ADO Connection Object
  • ADO Error Object
  • ADO Field Object
  • ADO Parameter Object
  • ADO Property Object
  • ADO Record Object
  • ADO Recordset Object
  • ADO Stream Object
  • ADO Data Types
  • Home
  • Courses
  • ASP And ASP.NET Tutorials
  • ASP The Global.asa File

ASP The Global.asa file

Previous Next

The 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 an ASP application.

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

The Global.asa file can contain only the following:

  • Application events
  • Session events
  • <object> declarations
  • TypeLibrary declarations
  • the #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 file.



Practice Excercise Practice now

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 for this is placed in event handlers. The Global.asa file can contain four types of events:

Application_OnStart - Occurs when the FIRST user calls the first page in an ASP application. This event occurs after the Web server is restarted or after 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 the ASP application.

Session_OnEnd - This event occurs EVERY time a user ends a session. A user-session ends after a page has not been requested by the user for a specified time (by default this is 20 minutes).

Application_OnEnd - This event occurs after the LAST user has ended the session. Typically, this event occurs when a Web server stops. This procedure is used to clean up settings after the Application stops, like delete records or write information to text files.

A Global.asa file could look something 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: Because we cannot use the ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we put subroutines inside an HTML <script> element.



Practice Excercise Practice now

TypeLibrary Declarations

A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.

Syntax

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

Parameter Description
file Specifies an absolute path to a type library.

Either the file parameter or the uuid parameter is required

uuid Specifies a unique identifier for the type library.

Either the file parameter or the uuid parameter is required

version Optional. Used for selecting version. If the requested version is not found, then the most recent version is used
lcid Optional. The locale identifier to be used for the type library

Error Values

The server can return one of the following error messages:

Error Code Description
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

Note: METADATA tags can appear anywhere in the Global.asa file (both inside and outside <script> tags). However, it is recommended that METADATA tags appear near the top of the Global.asa file.



Practice Excercise Practice now

Restrictions

Restrictions on what you can include in the Global.asa file:

  • You cannot display text written in the Global.asa file. This file can't display information
  • You can only use Server and Application objects 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



Practice Excercise Practice now

How To Use The Subroutines

Global.asa is often used to initialize variables. 

The example below shows how to detect the exact time a visitor first arrives on a Web site. The time is stored in a Session variable named "started", and the value of the "started" variable can be accessed from any ASP page in the application:

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

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

The example below shows how to redirect every new visitor to another page, in this case to a page called "newpage.asp":

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

And you can include functions in the Global.asa file.

In the example below the Application_OnStart subroutine occurs when the Web server starts. Then the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. The record set is assigned to an array, where it can be accessed from any ASP page 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>



Practice Excercise Practice now

Global.asa Example

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

  • The Application_OnStart sets the Application variable "visitors" to 0 when the server starts
  • The Session_OnStart subroutine adds one to the variable "visitors" every time a new visitor arrives
  • The Session_OnEnd subroutine subtracts one from "visitors" each time this subroutine is triggered

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

To display the number of current visitors in an ASP file:

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



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP