Introduction To The GLOBAL.ASA For ASP Programmers

I spent quite a bit of time as an ASP programmer before I learned about the GLOBAL.ASA file. Like many others, I learned ASP the old fashioned way. Already familiar with Visual Basic, the leap to VBScript was not difficult. Then I took a look at some sample ASP code, starting doodling with it, and next thing you know, I'm an ASP programmer.

Written by
Rating: 4 stars4 stars4 stars4 stars4 stars / 62
July 16, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


In my time writing ASP, I’ve designed and delivered some pretty complex systems for both intranet and Internet usage. There were some challenges that I ended up solving quite creatively, but could have been much easier to code had I been “in the know” about the GLOBAL.ASA file.

So, to help those of you just learning ASP, or even those who learned it the same way I originally did, I’ve put together this introduction as a tool to illustrate what the GLOBAL.ASA can do for you.

That being said, lets get down to what the GLOBAL.ASA file really is; an optional file that can contain declarations of objects, variables, and methods that can be accessed by any ASP page in an application. This file is stored in the root folder of your ASP application and must be named GLOBAL.ASA.

Now, I’m not going to be teaching any ASP in this article. The basic assumption is that you are familiar with ASP, and wish to learn more about using the GLOBAL.ASP file. There are many tutorials available freely on the Internet that will introduce you to ASP programming. Also, my examples will be supplied in VBScript, but any valid ASP scripting language may be used instead (JavaScript, Perl, etc).

Inside the GLOBAL.ASA File

Lets get a little more detailed about what can be contained inside the GLOBAL.ASA. For starters, this file can contain certain session and application events. These events tell the internal application and session objects what to do when they start and end. Below are the four possible events that may be included in this file:

• Application_OnStart – This event is called the FIRST time any user hits an ASP page in the same tree as the GLOBAL.ASA. The first hit of any user is considered to be when the application is first invoked. This function is good for loading any default values for application variables, and initializing logs files and such. The application state is reset after the Web Server is restarted, or when the GLOBAL.ASA file is modified. The Session_OnStart will automatically fire when this event finishes.

• Session_OnStart – This event is called the first time each new user hits the ASP application. So, for each session that is created, this event is called. This event is good for setting up any session level defaults or variables that will be needed for the duration of the user session.

• Session_OnEnd – This even will fire when a user ends a session. This can be done by a user logging out manually, or via a specified timeout period (by default 20 minutes). Useful for session cleanup code.

• Application_OnEnd – This final event occurs when the last user ends his or her session. This process normally fires when the web server is being shut down. This is useful for closing files, creating reports, and updating certain usage statistics.

Here is an example of using the GLOBAL.ASA to create a simple hit counter:


<SCRIPT language=vbscript runat="server">

Sub Application_OnStart
  getcounter
End Sub

Sub Session_OnStart
  Application
.Lock
  Application
("currentCount")=Application("CurrentCount")+1
  Application
.UnLock
End Sub

Sub Application_OnEnd
  Writecounter
End Sub

sub getcounter
  set conn
=Server.CreateObject("ADODB.Connection")
  
conn.Provider="Microsoft.Jet.OLEDB.4.0"
  
conn.Open "c:/webdata/counter.mdb"
  
set rs=conn.execute("select currentcount from datatable”)
  Application("
currentCount")=rs(“currentcount”)
  rs.Close
  conn.Close
end sub

sub writecounter
  set conn=Server.CreateObject("
ADODB.Connection")
  conn.Provider="
Microsoft.Jet.OLEDB.4.0"
  conn.Open "
c:/webdata/counter.mdb"
  conn.execute("
update datatable set currentcount “ &  
    
application(“currentcount”))    
  
conn.Close
end sub

</SCRIPT>
 



In order to use the counter value on any webpage, you would do the following:

 This is my web page There have been <%response.write(Application("currCount"))%>Visitors to this website



You will notice in the GLOBAL.ASA example above that I actually use subroutines that are called from the main events. I did this for code readability, and it is a common practice. You will also notice that there are no <% and %> tags inside the GLOBAL.ASA file. In this file, you must place all script within the <script> tag.

Application and Session Objects

Moving along, the next thing we can do inside of the GLOBAL.ASA is object declarations. In other words, you can create a reference to an object with session or application scope to be used by any of the ASP pages in the application. This can be accomplished by utilizing the <object> tag.

When declaring an object, you need to follow a specific syntax, as follows:


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



Follows are descriptions for each parameter:

• Scope – Sets the scope of the object. Can be either session or application.
• Id – A unique ID for the object.
• ProgID – The progID for your object, in the proper format.
• ClassID – The unique ID for the COM class object.


Note – Either a ProgID or a ClassID must be specified.

Here is an example of a GLOBAL.ASA with a declaration for the Microsoft Ad Rotator.


<SCRIPT language=vbscript runat="server">

Sub Application_OnStart
  
...
End Sub

Sub Application_OnEnd
  
...
End Sub
</SCRIPT>

<OBJECT id=AdObject progid="MSWC.AdRotator" scope="session" runat="server"></OBJECT>

You will notice that the <object> tag is outside of the <script> tag in the above example. This is the required format. To utilize the object in any ASP page, you would do the following:

 This is an advertising page <%=AdObject.GetAdvertisement("/myads.txt")%>


Type libraries for COM

The last use for the GLOBAL.ASA I would like to touch on, is the ability to specify Type libraries. This functionality was first introduced in IIS5, and is not available on earlier versions. It allows you to specify, using the <metadata> tag, a type library to bind to your ASP applications. This enables you to use the constants specified within the COM object that the type library references. An example of such follows:

 <!-- METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library"
TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}" -->



The above example would bind the Microsoft ADO 2.5 type library to your ASP application. This would enable you to use any of the constants referenced in the COM object, as follows:

 <%
rstTest.Open strSQLcnnTestADODB.adOpenStatic_
    ADODB
.adLockReadOnlyADODB.adCmdText
%>



In the above example, adOpenStatic, adLockReadOnly, and adCmdText are all constants used to describe states within the ADODB object. By referencing the type library these constants became available to all ASP pages. Alternately, an include file containing the constants could be included in all of your ASP pages, but this solution is more elegant and complete.

In have illustrated basic usage of the functionality available within the GLOBAL.ASA file. My examples only contained very basic concepts for ease of illustration, but you can do very complex logic within the GLOBAL.ASA, if the need arise. It is my belief that with the knowledge contained within this article you should be well equipped to begin using the GLOBAL.ASA file to compliment your current and future ASP applications.

About the author:
Rich Smith owns and operates Jamsoft Development, a programming firm who specializes in custom systems for small businesses.
blog comments powered by Disqus
ASP ARTICLES

- Using MySQL with ASP
- ADO for the Beginner
- ADO.NET 101: Data Rendering with a DataGrid ...
- Introducing SoftArtisans OfficeWriter 3.0 En...
- Getting Remote Files With ASP
- The Real Basics of Functions in ASP
- Enhancing Readability with ASP
- Mimicking PHP's String Formatting Functions
- Windows Server Hacks 12, 77, and 98
- How to Sort a Multi-Dimensional Array
- Developing an Information Management Tool wi...
- What are Active Server Pages?
- Getting Remote Pages with ASP
- FTP’ing Files with ASP
- Apply Single-Sign-On to Your Application

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials