wayne plourde

software architect mcad mcsd scjp

home | articles | book | resume | projects | contact

09 - Creating a Context for ASP Pages

ASP 3.0 The Complete Reference

Chapter 9

Creating a Context for ASP Pages

So far, all of the ASP examples we have shown have been restricted to a page scope. This means that the pages were unable to share information with other users of the application or with information specific to a user. This restriction would make it extremely difficult to develop an interactive web applications. Fortunately, ASP overcomes this, ASP by providing the Application and Session objects to establish a context that goes beyond the scope of the page.

The Application Object

The Application object provides a common context for all of the pages and users of an ASP application by providing access to shared data (sort of like global variables) and providing event handlers for the start and stop of the application. Shared resources are made available in two collections: Application.Contents and Application.StaticObjects. Both of these can be accessed using the default method as in the Request collection. The Application.Contents collection contains data elements that are assigned to the application during runtime. The StaticObjects collections contain object references that are created in the global.asa file with the <object> tag and set to application scope.

Although there is a global application in the default web site, we can easily create an application space that is separate and customized to the requirements of an individual application. There are two steps to setting up an application: first by configuring IIS and second by implementing a global.asa file.

Configuring an IIS Site as an Application

Whether you are using a virtual directory or a virtual web site for your application, the configuration is the same. We will begin by opening the properties dialog for either the virtual directory or the web site. Under the directory tab you will see the following:

Figure 1: IIS Configuration

If your application has not yet been configured, you will see the create button in the dialog box. To create an application, click the button and give the application a unique name. Once this is set, click the configuration button to set other application parameters.

Figure 2: IIS Configuration

Under the App options tab, you will find settings for your application. For most applications, the default settings are fine. However, if you are standardizing on JScript instead of VBScript, you will want to change this setting.

Sharing Application Data with Other Users.

Application variables can be written to and read from by simply accessing the variable name in the Application objects default Contents collection. New variables can be added to the application at any time. In the example below, we start with a simple form that allows someone to post a news item for the day. An ASP page receives the post and saves it to an Application variable.

news_post.html - HTML Form

1

2

3

4

5

6

7

8

9

<html>

<head><title>News Post Form</title></head>

<body>

<b>News Post Form</b><HR color=CCCCCC>

<form id=myForm action="news_post.asp" method="post" style="margin:0px;">

<textarea cols=40 rows=4 name="news"></textarea>

<HR color=CCCCCC>

<input type="submit" value="submit"><input type="reset" value="reset">

</form></body></html>

Figure 3: News Post Form

The form data is submitted to the following page and saved in an application variable along with a date stamp.

news_post.asp - VBScript ASP

1

2

3

4

5

<%

Application("news") = Request.Form("news")

Application("news_date") = Date

Server.Transfer("news_display.asp")

%>

Next, we jump to a second asp page that reads the current news item from the application and displays it to the user.

news_display.asp - VBScript ASP

1

2

3

4

5

6

7

<html>

<head><title>Application Variable Sample</title></head>

<body>

<b>News Bulletin</b> - <%= Application("news_date") %><hr color=CCCCCC>

<%= Application("news") %>

<hr color=CCCCCC>

</body></html>

Figure 4: News Bulletin from Application Variable

Of course, this is a rather simplistic example. You will probably want to save application data to a more permanent location in addition to the memory resident application variable. The reason being that if the application is shutdown, all the application variables are lost.

What can you put in the Application variables

All the basic datatypes of both VBScript and JScript are fair game for Application variables. When considering storing objects however, a number of warning alarms should go off. The good news is that JScript objects can be used freely. Objects derived from VBScript classes, however, cannot be saved in the application context. The reason for this is due to the fact that these objects reside within the instance of the scripting engine serving the page. When the page is complete so is the object. To protect you, IIS 5 will balk if you attempt to save VBScript objects into the Application context.

Objects derived from COM components are a whole new issue. Any of these objects can be saved to an application variable, however, if the object has not been designed for this purpose, it can restrict your application to only being able to handle one request at a time. For more on the issues that contribute to this, see the threading section of the previous Scripting with Objects chapter.

One final restriction, you cannot save an intrinsic object to an application variable. For instance, the following is illegal: Application(response)=Response.

Preventing concurrency problems.

Since we are in a multi-user application environment, it is possible that two pages could be trying to access the same application variable at the same time. Imagine the following. Two users hit a page in separate threads just milliseconds apart. The first users page reads the value from the application variable to increment it by 1. The second page reads the same value and increments it. The first page now writes the incremented value back to the variable and the second page writes the same value back to the variable. So two users have hit the page, but only one hit has been recorded because of the synchronicity and concurrency problem. For this reason, the Application object provides a Lock method. By using this method, only the person that has locked the application can have access to the application variables until the Application.Unlock is called. The following demonstrates how the counter can work with locking.

application_lock.asp - VBScript ASP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<%