ASP Application Object

Friday, August 24, 2012

tags ASP

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:


ContentsContains all the items appended to the session through a script command
StaticObjectsContains all the objects appended to the session with the HTML <object> element (i.e. in global.asa)


Contents.RemoveDeletes an item from the Contents collection
Contents.RemoveAll()Deletes all items from the Contents collection
LockPrevents the ability from modifying the variables in the Application object
UnlockRe-enables the ability to modify the variables in the Application object


Application_OnStartOccurs when the web application begins.
Application_OnEndOccurs when the web application ends.


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.



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.


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("users") = Application("users") + 1
  Application("subtotal") = Application("subtotal") + 1
End Sub

Sub Session_OnEnd
  Application("users") = Application("users") - 1
End Sub

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

ASP In A Nutshell Beginning ASP Databases Sams Teach Yourself Active Server Pages 3.0 in 21 Days