ASP ApplicationObject


A group of ASP files that work together to accomplish a task is called an application.


Application Object

An application on the Web may be a group of ASP files. These ASP files work together to accomplish a task. The Application object in ASP is used to bind these files together.

The Application object is used to store and access variables from any page, similar to the Session object. The difference is that all users share one Application object, while the relationship between Session objects and users is one-to-one.

The Application object holds information that will be used by many pages in an application (such as database connection information). This information can be accessed from any page. Also, you can change this information in one place, and the changes will automatically be reflected on all pages.


Store and Retrieve Application Variables

Application variables can be accessed and changed by any page in the application.

You can create Application variables in "Global.asa", as shown below:

<script language="vbscript" runat="server">

Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub

</script>

In the example above, we created two Application variables: "vartime" and "users".

You can access the value of an Application variable, as shown below:

There are
<%
Response.Write(Application("users"))
%>
active connections.


Loop Through the Contents Collection

The Contents collection contains all application variables. You can view the variables stored in it by looping through the Contents collection:

<%
dim i
For Each i in Application.Contents
Response.Write(i & "<br>")
Next
%>

If you do not know the number of items in the Contents collection, you can use the Count property:

<%
dim i
dim j
j=Application.Contents.Count
For i=1 to j
Response.Write(Application.Contents(i) & "<br>")
Next
%>


Loop Through the StaticObjects Collection

You can view the values of all objects stored in the Application object by looping through the StaticObjects collection:

<%
dim i
For Each i in Application.StaticObjects
Response.Write(i & "<br>")
Next
%>


Locking and Unlocking

You can use the "Lock" method to lock the application. When the application is locked, users cannot change Application variables (except the user who is currently accessing the Application variables). You can also use the "Unlock" method to unlock the application. This method removes the lock on Application variables:

<%
Application.Lock
'do some application object operations
Application.Unlock
%>

Other Extensions