Monday, March 22, 2010

Application Object of asp

An application on the Web may consists of several ASP files that work together to perform some purpose. The Application object is used to tie these files together. The Application object is used to store and access variables from any page, just like the Session object. The difference is that ALL users share ONE Application object (with Sessions there is ONE Session object for EACH user). The Application object holds information that will be used by many pages in the application (like database connection information). The information can be accessed from any page. The information can also be changed in one place, and the changes will automatically be reflected on all pages. The Application object's collections, methods, and events are described below: Collections Collection Description Contents Contains all the items appended to the application through a script command StaticObjects Contains all the objects appended to the application with the HTML tag Methods Method Description Contents.Remove Deletes an item from the Contents collection Contents.RemoveAll() Deletes all items from the Contents collection Lock Prevents other users from modifying the variables in the Application object Unlock Enables other users to modify the variables in the Application object (after it has been locked using the Lock method) Events Event Description Application_OnEnd Occurs when all user sessions are over, and the application ends Application_OnStart Occurs before the first new session is created (when the Application object is first referenced) Collections: ASP Contents Collection ________________________________________ The Contents collection contains all the items appended to the application/session through a script command. Tip: To remove items from the Contents collection, use the Remove and RemoveAll methods. Syntax Application.Contents(Key) Session.Contents(Key) Parameter Description key Required. The name of the item to retrieve ________________________________________ Examples for the Application Object Example 1 Notice that both name and objtest would be appended to the Contents collection: <% Application("name")="W3Schools" Set Application("objtest")=Server.CreateObject("ADODB.Connection") %> Example 2 To loop through the Contents collection: <% for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> or: <% For i=1 to Application.Contents.Count Response.Write(i & "=" & Application.Contents(i) & " ") Next %> Example 3 <% Application("date")="2001/05/05" Application("author")="W3Schools" for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> Output: date=2001/05/05 author=W3Schools ________________________________________ Examples for the Session Object Example 1 Notice that both name and objtest would be appended to the Contents collection: <% Session("name")="Hege" Set Session("objtest")=Server.CreateObject("ADODB.Connection") %> Example 2 To loop through the Contents collection: <% for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> or: <% For i=1 to Session.Contents.Count Response.Write(i & "=" & Session.Contents(i) & " ") Next %> Example 3 <% Session("name")="Hege" Session("date")="2001/05/05" for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> Output: name=Hege date=2001/05/05 ASP StaticObjects Collection ________________________________________ The StaticObjects collection contains all the objects appended to the application/session with the HTML tag. Syntax Application.StaticObjects(Key) Session.StaticObjects(Key) Parameter Description key Required. The name of the item to retrieve ________________________________________ Examples for the Application Object Example 1 To loop through the StaticObjects collection: <% for each x in Application.StaticObjects Response.Write(x & " ") next %> Example 2 In Global.asa: In an ASP file: <% for each x in Application.StaticObjects Response.Write(x & " ") next %> Output: MsgBoard AdRot ________________________________________ Examples for the Session Object Example 1 To loop through the StaticObjects collection: <% for each x in Session.StaticObjects Response.Write(x & " ") next %> Example 2 In Global.asa: In an ASP file: <% for each x in Session.StaticObjects Response.Write(x & " ") next %> Output: MsgBoard AdRot Methods: ASP Contents.Remove Method ________________________________________ The Contents.Remove method deletes an item from the Contents collection. Syntax Application.Contents.Remove(name|index) Session.Contents.Remove(name|index) Parameter Description name The name of the item to remove index The index of the item to remove ________________________________________ Examples for the Application Object Example 1 <% Application("test1")=("First test") Application("test2")=("Second test") Application("test3")=("Third test") Application.Contents.Remove("test2") for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> Output: test1=First test test3=Third test Example 2 <% Application("test1")=("First test") Application("test2")=("Second test") Application("test3")=("Third test") Application.Contents.Remove(2) for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> Output: test1=First test test3=Third test Examples for the Session Object Example 1 <% Session("test1")=("First test") Session("test2")=("Second test") Session("test3")=("Third test") Session.Contents.Remove("test2") for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> Output: test1=First test test3=Third test Example 2 <% Session("test1")=("First test") Session("test2")=("Second test") Session("test3")=("Third test") Session.Contents.Remove(2) for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> Output: test1=First test test3=Third test ASP Contents.RemoveAll Method ________________________________________ The Contents.RemoveAll method deletes all items from the Contents collection. Syntax Application.Contents.RemoveAll() Session.Contents.RemoveAll() Example for the Application Object <% Application.Contents.RemoveAll() %> Example for the Session Object <% Session.Contents.RemoveAll() %> ASP Lock and Unlock Methods ________________________________________ Lock Method The Lock method prevents other users from modifying the variables in the Application object (used to ensure that only one client at a time can modify the Application variables). Unlock Method The Unlock method enables other users to modify the variables stored in the Application object (after it has been locked using the Lock method). Syntax Application.Lock Application.Unlock Example The example below uses the Lock method to prevent more than one user from accessing the variable visits at a time, and the Unlock method to unlock the locked object so that the next client can increment the variable visits: <% Application.Lock Application("visits")=Application("visits")+1 Application.Unlock %> This page has been visited <%=Application("visits")%> times! Events: ASP Application_OnStart and Application_OnEnd Events ________________________________________ Application_OnStart Event The Application_OnStart event occurs before the first new session is created (when the Application object is first referenced). This event is placed in the Global.asa file. Note: Referencing to a Session, Request, or Response objects in the Application_OnStart event script will cause an error. Application_OnEnd Event The Application_OnEnd event occurs when the application ends (when the web server stops). This event is placed in the Global.asa file. Note: The MapPath method cannot be used in the Application_OnEnd code. Syntax ________________________________________ Examples Global.asa: To display the number of current visitors in an ASP file:

There are <%response.write(Application("visitors"))%> online now!