When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Monday, November 30, 1998

WebDaily: Your daily source for Web Technology Tips and Tricks!


*****************************************************************
Global.asa
*****************************************************************

* This article discusses what, exactly, Global.asa is, and how you can use it to increase the power of your Active Server Pages Applications

What is global.asa? You may have noticed that when you create a new web project with InterDev, a file is inserted into the root directory called global.asa. In this file, there are empty event handlers for events such as Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd. Global.asa is an optional file where you can define event scripts, and create objects that have either Session or Application scope. There can be at most one global.asa file per web application, and it must exist in the root directory of the web application.

As an Active Server Pages programmer, there are only two types of scope you need to know about: Session scope and Application scope. For an object declared in Session scope, each time a new user visits a page in the web application, the event Session_OnStart is fired, and the Global.asa function Session_OnStart is executed. The event Session_OnEnd fires when a set number of minutes have expired from when the user last accessed a page, or when the server calls the Abandon method.

The default session timeout is 20 minutes, but can be changed through registry modification, or by setting the Timeout property of the Session object. For example, in the global.asa file's Application_OnStart, you could write:

'Session will timeout in 5 minutes
Session.Timeout = 5

To explicitly end a Session, you just need to call the Abandon method, like so:

'This will clear the session
Session.Abandon

Session objects are maintained through the use of cookies, since each Session object is on a per user basis. This is the one of the major downsides of Session objects, that those who browse with cookies disabled do not "save" the Session-scoped objects you use. For example, if you have an ASP page that looks like this:

<%
Session("Hello") = "Hello, world!"
Response.Write(Session("Hello"))
%>

If the user has cookies enabled, he or she will see the words "Hello, world!" on their screen. Those who do not have cookies enabled will see nothing. Also, since all Session objects are created on a per visitor basis, and since they persist for usually 20 minutes after the last time a particular user visits a page, it is unwise to put any large objects in the Session, such as recordsets, arrays, etc. To learn more, visit this article, which deals with ASP optimization and responsible session-level programming.

Application scoped objects are created when the first user hits a web page in the web application, and are destroyed when the server shuts down. When the application is initiated, the Application_OnStart event is fired; when the server shuts down, Application_OnEnd is fired.

Global.asa, then, is a place for you to create any session or application level objects. Good things to put in your global.asa are database connection strings, counters, and small session-level variables. You want to stay away from putting large objects in session-level variables, such as ADO objects. Here is an example of what a Global.asa might look like:

<SCRIPT RUNAT=server LANGUAGE="VBScript">

sub Application_OnStart()

'Creates an application level connection string to database
Application("ConnectionString") = "DSN=pubs"
Application("UserID") = "sa"
Application("Password") = ""

'Used for counter
Application("VisitorCount") = 0
'So we know as of what date the counter was started
Application("CounterStart") = Now
end sub


sub Session_OnStart
'You need to lock the application for concurrency reasons
Application.Lock
Application("VisitorCount") = Application("VisitorCount") + 1
Application.UnLock
end sub

</SCRIPT>

This global.asa file sets up an application-scoped counter, and increments it each time a new session is created (that is each time a new visitor stops by).

I know that this article may seem a bit wordy and difficult to follow; for that I appologize. If you have any questions, comments, inqueries, statements, or ideas you'd like to ask, make, inquire about, declare, or share, please simply reply to this email or direct your browser to our add tips section.

Happy Programming!

*****************************************************************
To subscribe to WebDaily, point your browser to:
http://www.4GuysFromRolla.com/webtech/webdaily
*****************************************************************
*****************************************************************

Please forward this information to a friend, including the subscription URL! Happy Programming!


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article