HomeASP Introduction To The GLOBAL.ASA For ASP Pro...
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.
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).
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.
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:
• 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:
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:
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:
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.