An ASP application is a term that generally refers to a grouping of several ASP files that interact with the web visitor. The Application object can be used to
store and access variables from any page, by any visitor. It is similar to the Session object, except that all users share the one application
object. Session objects are unique to each visitor.
The data that is stored in the Application object can be accessed from any page as well as modified/updated from any page. The updated changes are accessible by all
of your visitors. While this is not meant as a replacement for storing data in a database, the Session and Application object is helpful when storing data temporarily about
the users on your site. The Application Object's collections, methods, and events are as follows:
Collections
Collection | Description |
Contents | Contains all the items appended to the session through a script command |
StaticObjects | Contains all the objects appended to the session with the HTML <object> element (i.e. in global.asa) |
Methods
Method | Description |
Contents.Remove | Deletes an item from the Contents collection |
Contents.RemoveAll() | Deletes all items from the Contents collection |
Lock | Prevents the ability from modifying the variables in the Application object |
Unlock | Re-enables the ability to modify the variables in the Application object |
Events
Event | Description |
Application_OnStart | Occurs when the web application begins. |
Application_OnEnd | Occurs when the web application ends. |
Examples
Store and Retrieve Session Variables
The most common uses for the session object are to store and retrieve information. You create and/or modify session variable information by specifying the variable name within quotes. If you assign string data to the session, encapsulate that
data in quotes as well. If you want to assign numeric data, quotes are not needed.
<%
Application("name")="itgeared"
%>
<%
Response.Write(Application("name"))
%>
Remove Session Variables
You can easily remove one variable from the Contents collection or all of the variables using one command. Use either the Remove or RemoveAll method.
<%
Application.Contents.Remove("name")
Application.Contents.RemoveAll()
%>
Global.asa Events
In this next example, we can use the Application_OnStart and Application_OnEnd events (defined in the Global.asa file) to maintain a count of the active users accessing our web server.
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("users") = 0
Application("subtotal") = 0
Application("total") = 0
End Sub
Sub Application_OnEnd()
Application("total") = Application("subtotal")
End Sub
Sub Session_OnStart
Application.Lock
Application("users") = Application("users") + 1
Application("subtotal") = Application("subtotal") + 1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("users") = Application("users") - 1
Application.UnLock
End Sub
</script>
In your ASP page...
<%
Response.Write("There are " & Application("users") & " users online.")
%>
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources