You can use the Application object to share information among all users of a given application. An ASP-based application is defined as all the .asp files in a virtual directory and its subdirectories. Because the Application object can be shared by more than one user, there are Lock and Unlock methods to ensure that multiple users do not try to alter a property simultaneously.
The Lock method prevents other clients from modifying Application object properties.
The Unlock method allows other clients to modify Application object properties.
Scripts for the preceding events are declared in the Global.asa file. For more information about these events and the Global.asa file, see the Global.asa Reference.
You can store values in the Application object. Information stored in the Application object is available throughout the application and has application scope. The following script demonstrates storage of two types of variables.
<% Application("greeting") = "Welcome to My Web World!" Application("num") = 25 %>
However, if you store an object in the Application object, and use Visual Basic® Scripting Edition as your primary scripting language, you must use the Set keyword. This is illustrated in the following script.
<% Set Application("Obj1") = Server.CreateObject("MyComponent") %>
You can then reference the methods and properties of MyObj on subsequent Web pages, by using the following:
<% Application("Obj1").MyObjMethod %>
Or by extracting a local copy of the object and using the following:
<% Set MyLocalObj1 = Application("Obj1") MyLocalObj1.MyObjMethod %>
Another way to create objects with application scope is by using the <OBJECT> tag in the Global.asa file. For more information, see the Global.asa Reference.
You cannot, however, store a built-in object in the Application object. For example, each of the following lines returns 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 should know what threading model it uses. Only objects marked as both free- and apartment-threaded can be stored in the Application object. For more information, see Threading Models in Creating Components for ASP.
If you store an array in a Application object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:
<% Application("StoredArray")(3) = "new value" %>
This is because the Application object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value is indexed into the collection, overwriting any information stored at that location.
It is strongly recommended that if you store an array in the Application object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the Application object all over again, so that any changes you made are saved. This is demonstrated in the following example:
---file1.asp--- <% 'Creating and initializing the array dim MyArray() Redim MyArray(5) MyArray(0) = "hello" MyArray(1) = "some other string" 'Storing the array in the Application object Application.Lock Application("StoredArray") = MyArray Application.Unlock Response.Redirect("file2.asp") %> ---file2.asp--- <% 'Retrieving the array from the Application Object 'and modifying its second element LocalArray = Application("StoredArray") LocalArray(1) = " there" 'printing out the string "hello there" Response.Write(LocalArray(0)&LocalArray(1)) 'Re-storing the array in the Application object 'This overwrites the values in StoredArray with the new values Application.Lock Application("StoredArray") = LocalArray Application.Unlock %>
Application("NumVisits") = Application("NumVisits") + 1
%> This application page has been visited <%= Application("NumVisits") %> times!
The preceding example uses the application variable NumVisits to store the number of times that a particular page has been accessed. The Lock method is called to ensure that only the current client can access or alter NumVisits. Calling the Unlock method then enables other users to access the Application object.
© Microsoft Corporation. All rights reserved.
|file: /Techref/language/asp/obj/introbj_1.htm, 6KB, , updated: 1996/11/21 19:01, local time: 2019/10/19 19:05,
|©2019 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?|
<A HREF="http://www.sxlist.com/techref/language/asp/obj/introbj_1.htm"> Application Object</A>
|Did you find what you needed?|
Welcome to sxlist.com!
& kind contributors
just like you!
Please don't rip/copy
Copies of the site on CD
are available at minimal cost.
Welcome to www.sxlist.com!