The ASP Application object shares information among all users of a given application. An ASP-based application is defined as all the .asp files in a virtual directory and associated sub-directories. Because the Application object can be shared by more than one user, Lock and Unlock methods are provided to ensure that multiple users do not try to alter a property simultaneously.
Prevents script from modifying object properties.
Enables script to modify object properties after execution of the Lock method.
Runs when an ASP page belonging to the application is accessed for the first time.
Runs when the Web server is shut down.
Values can be stored in the Application object. These values are available throughout the application and have application scope.
Objects can be created within the Application_OnStart script and assigned to the Application object. You cannot, however, store a built-in object in the Application object. Each of the following lines will return an error:
Set Application("var1") = Session
Set Application("var2") = Request
Set Application("var3") = Response
Set Application("var4") = Server
Set Application("var5") = Application
Before you store an object in the Application object, you must know what threading model it uses. Only objects marked as both free and apartment-threaded can be stored in the Application object.
The Application object is implemented as a collection. If you store an array in an Application object, you should not attempt to alter elements of the stored array directly. For example, the following script does not work:
Application("StoredArray") (3) = "new value"
Instead of storing the value "new value" in StoredArray(3) the value is stored in the Application collection, overwriting any information stored at Application(3).
It is strongly recommended that if you store an array in the Application object that you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done making changes to the array, store the array back into the Application object to save changes. This is demonstrated in the following examples.
You can store different types of variables:
Application("greeting") = "Welcome to My Web World"
Application("num") = 25
You must use the Set keyword when storing objects:
Set Application("Obj1") = Server.CreateObject("MyComponent")
You can use methods and properties on subsequent ASP pages by using the following:
As an alternative, you can extract a local copy of the object:
Set MyLocalObj1 = Application("Obj1")
The next example demonstrates using an application variable called NumVisits to store the number of times a particular page has been accessed. The Lock method is called to ensure only the current client can access or alter NumVisits. Calling the Unlock method then enables other clients to access the application object.
Application("NumVisits") = Application("NumVisits") + 1
This application page has been visited <%= Application("NumVisits") %> times!
The next three examples demonstrate storing and manipulating an array in the Application object. The Lock and Unlock methods are used to control access to the Application object.
Application("StoredArray") = MyArray
To retrieve the array from the Application object and modify its elements:
LocalArray = Application("StoredArray")
LocalArray(0) = "Hello"
LocalArray(1) = "there"
Next you need to restore the array in the Application object. This overwrites the values in StoredArray with new values.
Application("StoredArray") = LocalArray
Copyright 2002 Sun Microsystems, Inc. All rights reserved. Legal Notice.