Page

8.2.3- Application Object Methods

Created by Brendan Doss.
Last Updated by Jim Minatel.  

PublicCategorized as 08. Applications, Sessions and Cookies.

Not yet tagged
<< 8.2.2- Application Object Collections Chapter8 8.2.4- Global.asa >>

Application Object Methods

The Application object has two methods (and two further methods via the Contents collection):

 

  • Lock
  • UnLock

Lock Method

The Lock method prevents clients – other than the one currently accessing it – from modifying the variables stored in the Application object. This means you can't get one client changing the variables, taking a long time over it, and another one changing the same variables to a different set of values in the time between. This way data corruption can be avoided.

UnLock Method

This method removes the lock from variables stored in the Application object, freeing them up after Application object has been locked previously using the Application.Lock method.

 

The Contents collection itself has a further two methods:

 

  • Remove
  • RemoveAll

Remove Method

Removes an item from the Contents collection. For example, look at the following:

 

Application("FirstVariable") = "Cats"

Application("SecondVariable") = "Dogs"

Application.Contents.Remove("FirstVariable")

 

This would create two Application objects and then remove only the first.

RemoveAll Method

Removes all items from the Contents collection. For example, consider the following:

 

Application("FirstVariable") = "Cats"

Application("SecondVariable") = "Dogs"

Application.Contents.RemoveAll

 

This creates two Application objects and then removes the collection along with any other Application objects that might have been created prior to this.

Try It Out – Working with Application-Level Variables

In this example, we will take a look at an ASP script that will demonstrate how to interact with the application-level variables that were created in the global.asa file that we looked at earlier.

 

1.    Create a new file, and enter the following:

<HTML>

<HEAD>

<TITLE>Application Variable Test</TITLE>

</HEAD>

 

<BODY>

Let's retrieve the values of the Application Variables:<P>

myAppVariable = <%= Application("myAppVariable")%><BR>

anotherAppVariable = <%= Application("anotherAppVariable")%><HR>

 

Now, let's set the variables:<HR>

<%

Response.Expires = 0  

Application.Lock

 Application("myAppVariable") = Now

 Application("anotherAppVariable") = CStr(CInt(Application("anotherAppVariable"))+1)

 Application.UnLock

%>

Variables set - <A href="appVarTest.asp">click here</A> to reload the page to view

</BODY>

</HTML>

 

2.    Save the file as appVarTest.asp to the BegASPFiles directory.

3.   View the file in your web browser. The first time you view the page the variables will be blank.

 

Chapter8_image008

 

If on your browser anotherAppVariable is also blank like myAppVariable, then your file isn't correctly locating global.asa. You need to make sure global.asa and appVarTest.asp are both in the BegAsp folder, and that this folder is set up as an application (mentioned in chapter 1 ).

 

4.    Click on Refresh or link at the bottom to view the contents.

 

Chapter8_image009

 

5.    Open another copy of your browser and view the file again.

Chapter8_image010

How It Works

In this example, we are using the application-level variables that were created in the global.asa example that we ran earlier.

 

<BODY>

Let's retrieve the values of the Application Variables:<P>

myAppVariable = <%= Application("myAppVariable") %><BR>

anotherAppVariable = <%= Application("anotherAppVariable") %><HR>

First, we will retrieve the values that are currently stored in the two application-level variables that we have declared. Since all we are doing is accessing the value stored in the variables, there is no need to call the Lock method.

 

Now, let's set the variables:<HR>

<%

Response.Expires = 0  

 Application.Lock

 Application("myAppVariable") = Now

 Application("anotherAppVariable") = CStr(CInt(Application("anotherAppVariable"))+1)

 Application.Unlock

%>

 

So that the browser doesn't cache the page and retrieve it from the cache each time you click, we've started with setting Response.Expires = 0  to cause the page to be refershed with each access. The next step is to reset the values of the two application-level variables. In order to give this script exclusive control over the variables, we first need to call the Lock method of the Application object. This will give total control of all application-level variables to this script until the corresponding call to UnLock is made. Since there can be multiple users accessing a web site at the same time, and possibly accessing the same page, we need to take these precautions to ensure that the data is updated properly.

 

Now that we have exclusive control over the variables, we can update their values. The value of the myAppVariable variable is set to the current date and time. The value that is stored in anotherAppVariable is a string containing the number of times this page has been accessed. Here, we want to increment this count by one. We could rely on VBScript's automatic type conversion, but to make our code more readable by someone trying to learn what it is doing, we will explicitly convert the variable's type.

 

The first step is to convert the string value that is stored in the application-level variable to an integer value. This is done using the CInt function. Now that we have an integer value, we can add 1 to it to get the new count. Finally, to store the value back into the application-level variable, we will convert the new count back to a string using the CStr function.

 

With all of the changes successfully made, we will release our hold on the application-level variables by calling the Unlock method of the Application object.

 

Now that we have learnt some of the basics of the Application object, let's take a more in-depth look at the global.asa file.

<< 8.2.2- Application Object Collections Chapter8 8.2.4- Global.asa >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |