Did you find this useful?
Socialize it today.


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:

Collections

CollectionDescription
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)


Methods

MethodDescription
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


Events

EventDescription
Application_OnStartOccurs when the web application begins.
Application_OnEndOccurs 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.")
%>


Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

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