Home

ASP ADVANCED

Like the Session object, the Application object has two events: Application_OnStart and Application_OnEnd. One event is fired when an Active Server Pages application starts; the other event is fired when it ends.

When does an application start?  Not, as you might expect, immediately after you start the Web server.  An application doesn't start until the first page is requested from the application.

An Application_OnStart event will always occur before a Session_OnStart event.  However, unlike a Session_OnStart event, the Application_OnStart event is not fired whenever a new visitor requests a page from the application.  The Application_OnStart event only fires once-­when the first visitor arrives.

The Application_OnEnd event is fired when the Web server is shut down or the application is unloaded.  For example, if you use the Internet Service Manager to turn off the Web service, the Application_OnEnd event will be fired.  If you're running an application as a separate process, this event is also triggered when the application is unloaded using the Unload button.  An Application_OnEnd event always occurs after the last Session_OnEnd event.

The Application_OnStart and Application_OnEnd events each trigger one and only one script.  Both of these scripts must be located in the Global.asa file.  These special scripts can't be called from any other Active Server Page.

The Global.asa file is a special file that resides in the root directory of an application.  Each application can have only one of these files.  The Global.asa file contains all the scripts and objects that are global to an application.  The file has the following structure:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnStart
END SUB
</SCRIPT>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnEnd
END SUB
</SCRIPT>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnStart
END SUB
</SCRIPT>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnEnd
END SUB
</SCRIPT>

You learned about two of the Global.asa scripts in before Chapters the Session_OnStart and Session_OnEnd events are used with sessions.  The remaining two scripts, however, are triggered by the two events of the Application object.

You are very restricted in what you can include in these Application_OnStart and Application_OnEnd scripts.  You can't place any statements that output content.  For example, you can't use HTML or the Response.Write method.  Furthermore, you must be cautious with the object you use within the Application_OnStart or Application_ OnEnd scripts.

The Application_OnStart script is valuable for initializing application_wide variables.  For example, one common use of the Application_OnStart and Session_OnStart scripts is for tracking the total number of visitors since the application was started.  Here's an example of how you can do this:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnStart
Application("TotalUsers")=0
END SUB
</SCRIPT>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnStart
Application.Lock
Application("TotalUsers")=Application("TotalUsers")+1
Application.Unlock
END SUB
</SCRIPT>

A single statement has been added to the Application_OnStart script.  The statement initializes the application variable named TotalUsers to zero.  This script is executed only when the Web server is first started.

The Session_OnStart script has been modified to increment TotalUsers whenever a new user arrives at the Web site.  Notice how the application variable is first locked before it's modified.  This prevents conflicts when more than one new user arrives at the same time.

After you've modified the Global.asa file, you can display the total number of visitors on any Active Server Page by including the following line:

<%=Application("TotalUsers") %>