Building WebApps  «Prev  Next»

Lesson 4Application-level Variables
ObjectiveDescribe uses for application variables with multiple users.

Application-level Variables

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.
Here are some additional ways to use application variables[1] in your Web application.

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:

<TABLE>
<TR>
<TD>Total number of current users:</TD>
<TD><%= Application("aiUserCount")%></TD>
</TR<
</TABLE>

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;
 pwd=password"
Application("acWebHome") = "http://www.mywebserver.com/"
Application("acSecureServer") = "https://www.mywebserver
 .com/SSL/"

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:

<HTML>
<BODY>
<IMG SRC="<%=Application("acImages_Dir")%>imagename.gif"
 BORDER=0>
</BODY>
</HTML>

Which has an HTML output of
<HTML>
<BODY>
<IMG SRC="http://www.mywebserver.com/images/imagename.gif"
 BORDER=0>
</BODY>
</HTML>

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.

[1]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.