Home

ASP ADVANCED

The Application object has all the collections, methods, and events related to applications.  In the following sections, you learn how to use the Application object to create application variables and application events.

An Introduction to Application Variables

An application variable contains data that can be used on all the pages and by all the users of an application.  Application variables, can contain any type of data, including arrays and objects.  An application variable differs from a session variable in two ways:

  • Unlike a session variable, an application variable doesn't depend on cookies.  The Web server doesn't need to track user sessions to use application variables.  This means that using them is risk-free, because they're compatible with all browsers.
  • Unlike a session variable, the data in an application variable can be shared among multiple users.  Data can be retrieved from one user and stored in an application variable to be read by another user.

Following are some common uses for application variables:

  • An application variable can be used to display transient information on every Web page.  For example, you can use an application variable to display a "tip of the day" or a daily news update on every Web page.

  • An application variable can be used to record the number of times that a banner advertisement on your Web site has been clicked. (You learn how to do this in Chapter , "Working with Advertisements.")

  • An application variable can hold data retrieved from a database.  For example, you can retrieve a list of items for sale at your Web site from a database and display this list on multiple pages using an application variable.

  • An application variable can contain a running count of the number of visitors at your Web site.  You learn how to do this in the later section "The WhosOn Page."

  • An application variable can be used to enable communication between the users of your Web site.  For example, you could use application variables to create multiuser games or multiuser chat rooms.  An example of a chat page using application variables is provided in the later section "The Chat Page."

Creating and Reading Application Variables

Creating application variables is easy.  To create a new application variable, you can simply pass the name of the new variable to the Application object, as in the following example:

<HTML>
<HEAD><TITLE> Application Example </TITLE></HEAD>
<BODY>
<%
Application("Greeting")="Welcomel"
%>
<%=Application("Greeting")%>
</BODY>
</HTML>

In this example, a new application variable named Greeting is created and assigned the value "Welcome!". Finally, the value of the new application variable is outputted to the browser retrieving the page.

Once an application variable has been assigned a value, the value can be displayed on all the pages in an application.  For example, the following page would also display the greeting, even though the Greeting variable hasn't been assigned a value on this page:

<HTML>
<HEAD><TITLE> Another Page </TITLE></HEAD>
<BODY>
<%=Application("Greeting")%>
</BODY>
</HTML>

It's important to understand that, unlike session variables, application variables don't die when a user leaves.  Once an application variable has been assigned a value, it retains that value until the Web server is shut down or the application is unloaded.  If you're lucky, this could be weeks or even months.

Because application variables aren't destroyed automatically when a user leaves, you must be careful not to go wild with creating them.  Application variables use memory; you should use them sparingly.

It's also important to understand that an application variable is not relative to a particular user.  If one user requests a Web page that assigns one value to an application variable, and another user requests a page that assigns another value, the value of the variable will change for both users.  Consider the following script:

<%
Randomize
If INT(2*RND)=1 THEN
Application("FavoriteColor")="Blue"
ELSE
Application("FavoriteColor")="Red"
END IF
%>

This script randomly assigns the value "Blue" or "Red" to the application variable named FavoriteColor.  Suppose two users retrieve a page with this script.  In that case, the value of the variable would be the same for the two users.  The variable would have whatever value was assigned to it when the second user retrieved the page.  Potentially, this could create a problem.  Because more than one user can access an application variable at the same time, conflicts could arise.  For example, suppose you're using an application variable to record the number of times a banner advertisement has been clicked.  Every time the advertisement is clicked, a script like the following is executed:

<%
NumClicks=Application("BannerClicks")
NumClicks=NumClicks+1
Application("BannerClicks")=Numclicks
%>

The script simply increments the number stored in the application variable BannerClicks by 1. But suppose two users click an advertisement at the same time.  The same script would be executed at the same time for both users.  If this happens, the value of BannerClicks will be inaccurate.  Both users will have incremented the variable to the same value.

Fortunately, the Application object has two methods that can help in precisely this type of situation.  The Lock and Unlock methods are used to temporarily prevent other users from changing the value of an application variable.  Here's the previous example, rewritten to prevent any potential conflicts:

<%
Application.Lock
NumClicks=Application("BannerClicks")
NumClicks=NumClicks+1
Application("BannerClicks")=Numclicks
Application.Unlock
%>

The first line in the script locks all the variables in the application object.  When the variables are locked, other users can't modify them until they're unlocked.  The application variables remain locked until the unlock method is explicitly called (as in the preceding example) or until the end of the page is reached.

Notice that you can't lock application variables selectively; it's an all-or-none choice.  The preceding script temporarily prevents other users from modifying all the application variables that may exist.

It's important to understand that locking the application variables doesn't permanently prevent other users from modifying the variables.  Locking simply forces any modifications to take place in an orderly fashion.  The application variables are modified serially rather than haphazardly.