| << 8.2.0- The Application Object | Chapter8 | 8.2.2- Application Object Collections >> |
Application Variables
One of the features of an application is that you can store information that is available to all clients that are accessing the application. This information is stored in what is known as an application-scope variable.
To initialize variables in the Application object, you store the information about them in a special ASP file named global.asa. Each application can have only one global.asa, and it's placed in the application's root. To make sure global.asa is read by ASP, it isn't enough just to make sure that it is stored in a virtual directory, you have to explicitly have created the application virtual directory it is stored in using the Properties Dialog in MMC, as we did in Chapter 1 . If you've created one, then the button will read Remove.
|
|
Otherwise you will need to click on Create to create the application.
We will cover the items that can be placed in global.asa later in this chapter, but we will introduce what we need here to get us started on application-level variables.
Try It Out – Your First global.asa
In this example, we will create a very basic global.asa file so that we can declare some application-level variables. Later in this section, we will access these variables from an ASP page in our application.
1. Create a new file, and write the following:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
Application("myAppVariable") = " "
Application("anotherAppVariable") = 0
End Sub
</SCRIPT>
2. Save the file as global.asa in your BegASPFiles directory.
If you are using FrontPage 98 or another application that doesn't recognize .asa as a 'valid' file extension, then you can change this yourself by going to Windows Explorer's View | Folder Options | File tab and typing in the following settings:
Description - ASA File
Extension - ASA
Content type - <leave blank>
Opens with - Notepad (or your choice here)
What It Does
In our first global.asa file, we are declaring some application-level variables that we will use later in our examples. The first thing that you will notice about this file is that there are no <%…%> blocks. As you have seen before, these <% and %> tags are used to indicate ASP script within a file. In the global.asa file, we will be using the following syntax instead (which can also be used in ASP files):
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
The procedures that are part of global.asa are defined within a script block. The language that we are using for scripts is VBScript. Since these scripts will be run at the server, as opposed to the client, we have included the RUNAT parameter, and passed it a value of Server. If you include script that is not enclosed by <SCRIPT> tags, the server returns an error, as it won't be marked up for server-side execution.
Sub Application_OnStart
Application("myAppVariable") = " "
Application("anotherAppVariable") = 0
End Sub
We've used one event (there are a possible four, which we'll discuss shortly) in our global.asa file and this is Application_OnStart. This event will be fired only once when the first visitor hits the page. So, an application is started the first time one of its pages is accessed by a user. The Application_OnStart event occurs before the first new session is created, that is, before the Session_OnStart event.
Inside of this event handler, we are initializing two application-level variables. Application-level variables are actually elements of the Application object. We set and retrieve their values in the same way that we set and retrieve the values in a collection.
In this example, we have created two application-level variables:
- myAppVariable
- anotherAppVariable
The myAppVariable variable has been initialized to an empty string. The anotherAppVariable variable has been initialized to a string containing 0. Once that is complete, the event handler has finished, and we can complete the global.asa file with a closing </SCRIPT> tag.
| << 8.2.0- The Application Object | Chapter8 | 8.2.2- Application Object Collections >> |

RSS


