The ASP Application object manages application-scope variables available to all users of the ASP application. These variables can hold literals or even objects.

An ASP application is a virtual directory tree in the web server, i.e. all the files in a root directory and its sub-directories.

Basic Usage

An application-scope variable can be set by adding to the Application.Contents collection. EG:

Application("strX") = "Hello world!"
Set Application("objX") = Server.CreateObject("myComponent")

Items may be added or removed from the Application.Contents collection using either of two methods. EG:

Application.Contents.Remove("strX")
Application.Contents.Remove(2)
Application.Contents.RemoveAll()

Application-scope variables might be used in any ASP page in the ASP application. EG:

Response.Write "I said: " & Application("strX")
Application("objX").MethodOfComponent
Set myObject = Application("objX")
myObject.MethodOfComponent

The Application.Contents collection can be looped through like most collections. EG:

For Each Key in Application.Contents
    ...
Next
For intItem = 1 to Application.Contents.Count
    ...
Next

To prevent problems with simultaneous access of application-scope variables, use the Application.Lock and Application.Unlock methods. EG:

Sub SIncrementBigCount()
    Application.Lock
    Application("BigCount") = Application("BigCount") + 1
    Application.Unlock
End Sub

Application-scope variables can be dealt with when the ASP application starts and ends by putting code into the Global.asa file. Of the ASP built-in objects, only Application and Server objects will be available. The Application.Lock and Application.Unlock methods are not needed in the Global.asa file. Here is an example of Application_OnStart code:

Sub Application_OnStart
    Application("BigCount") = 0
End Sub

Collections

Contents

Collection of all application-scope variables added via script.

StaticObjects

A collection similar to the Application.Contents collection except that these are all objects created by the <object> tag.

Methods

Contents.Remove(key)

Remove a specific variable from the Application.Contents collection.

Contents.RemoveAll()

Removes all variables from the Application.Contents collection.

Lock()

Locks the Application object so there are no conflicts because of simultaneous access.

Unlock()

Unlocks the Application object. If Application.Unlock is not called explicitly, then the Application object is unlocked when the .asp file ends or times out.

Events

Application_OnEnd

Occurs when the ASP application ends, after the Session_OnEnd event. An application ends when the server shuts down. The event script is in the Global.asa file.

Of the ASP built-in objects, only the Application, Server, and Session objects are available to the Session_OnEnd event script.

Application_OnStart

Occurs when the ASP application starts, before the Session_OnStart event. An application starts when a file in the virtual directory is accessed. The event script is in the Global.asa file.



GeorgeHernandez.comSome rights reserved