|Lesson 4||Application-level Variables|
|Objective||Describe uses for application variables with multiple users.|
The ASP Application Object you saw referenced in prior code segments as
Application ("aiUserCount"), makes it easier to share information among all the users who are using the same application at the same time.
Summary of the ASP Application Object
Here are some additional ways to use application variables in your Web application.
| Application Summary|
| Collections |
| All the Application variables (and their values) are stored in the Contents collection. |
| All the objects created with the <OBJECT> tag, but not those created with the CreateObject() method of he Server object. |
| Events |
| The procedure that runs when an application ends (after the last Session_OnEnd procedure has run). The code for this procedure, if you decide to write any, must stored in the Global.asa file. |
| The procedure that runs when an application begins (before the first Session_OnStart procedure begins. Like the other Session and Application events, the code must stored in the Global.asa file. |
| Methods |
|Prevents other scripts from modifying any part of an application, until either the Unlock() method is called, or the current ASP file ends or times out. |
| Re-enables access to an Application object, after it was locked. |
Displaying the application user count
Continuing the prior example, suppose you had created the application variable
Application("aiUserCount") to track the number of current users. You could then refer to this variable using the Application object and the variable name,
as shown below in an HTML segment that displays the current user total on the user's browser:
<TD>Total number of current users:</TD>
Using application variables for external parameters
It is a good Web application practice to store external parameters that occasionally change in an Application variable.
Examples of this are:
Application("SMTPname") = "smtp.mywebserver.com"
Application("ConnectionString") = "dsn=MyDSN;uid=UserID;
Application("acWebHome") = "http://www.mywebserver.com/"
Application("acSecureServer") = "https://www.mywebserver
If you explicitly code external parameters, such as mail servers or your Web site's root URL, into .html and .asp files, should these later change, you will have to locate their instances in each of the individual files in order to update them all. Failing to catch one or two references may mean that an image or an entire html or asp page will be "broken" and won't appear or run. By putting each one into an Application variable, one change to the
Application_OnStart procedure that initializes them makes that change throughout the Web site.
Using an application variable for image location
The incorporation of images into our Web application presents another opportunity to use a variable to simplify site maintenance.
For our project, let's assume that we have pictures of each of our T-Shirts. To keep things organized, we would not want to store the images in the same directory as our .asp and .html files. Let's assume our images are stored in a new directory called
/images. We could reference all our images like this:
Which has an HTML output of
Now, if for any reason we change the location of the images, we need only change the Global.asa file, not each individual .asp file. Remember, this only works with .asp files, not with .html files. The next lesson explains how application variables may be incorrectly updated.
Application variables: A variable defined in the global.asa file, which can be read by any ASP script in the same virtual directory as the global.asa file.