In order to work properly, they must be placed in a virtual application on the server, and the provided global.asa file must be placed in the root directory of the application. The simplest way is to put the global.asa file in the root directory of the default Web site (C:/InetPub/WWWRoot by default).
It is a good idea to rename any existing global.asa files so that they can be restored later.
1. Displaying the contents of the Application collection
The ASPCounter object is a member of the StaticObjects collection (defined through the <OBJECT> element), but the rest (instantiated by Server.CreateObject) is a member of the Contents collection.
You can see the values put into these collections using the global.asa example web page, which we saw earlier:
<!-- Declare instance of the ASPCounter component with
application-level scope //-->
<OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin”
PROGID=”MSWC.Counters”>
</OBJECT>
...
...
<SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”>
Sub Application_onStart()
'Create an instance of an ADO Connection with application-level scope
Set Application("ADOConnection") = Server.CreateObject("ADODB.Connection")
Dim varArray(3) 'Create a Variant array and fill it
varArray(0) = "This is a"
varArray(1) = "Variant array"
varArray(2) = “stored in the”
varArray(3) = "Application object"
Application("Variant_Array") = varArray 'Store it in thd Application
Application(“Start_Time”) = CStr(Now) 'Store the date/time as a string
Application(“Visit_Count”) = 0 'Set counter variable to zero
End Sub
...
...
</SCRIPT>
(1) Code for traversing the Contents collection.
In order to traverse the Contents collection, you can use a For Each...Next structure. Each item in the collection can be a simple Variant type variable, an array of Variant, or a reference to an object. Because each type of value needs to be handled differently, each one has to be checked to determine its type.
You can use the VarType function in VBScript to accomplish this work. Use the IsObject and IsArray functions instead:
For Each objItem in Application.Contents
If IsObject(Application.Contents(objItem)) Then
Response.Write "Object reference: '" & objItem & "'
"
ElseIf IsArray(Application.Contents(objItem)) Then
Response.Write "Array: '" & objItem & "' contents are:
"
VarArray = Application.Contents(objItem)
'Note: the following only works with a one-dimensional array
For intLoop = 0 To UBound(varArray)
Response.Write “ Index(“ & intLoop & “) = “ & _
VarArray(intLoop) & “
"
Next
Else
Response.Write "Variable: '" & objItem & "' = " _
& Application.Contents(objItem) & “
"
End If
Next
notice how the program retrieves this array from the Application object. Assign it to a local (Variant) variable and use the following statement:
varArray = Application.Contents(objItem)
Use the UBound function to find out the size of the array (number of elements). This value can be used as the termination condition for the traversal:
For intLoop = 0 UBound(varArray)
This example is a one-dimensional array and will only display the contents of such an array. Edit the code as needed to handle multidimensional arrays, for example:
For intLoop = 0 To UBound(varArray)
IntNumberOfDimensions = UBound(varArray, 1)
For intDimension = 0 To intNumberOfDimensions
Response.Write “ Index(“ & intLoop & “) = “ _
& varArray(intLoop, intDimension)
Next
Response.Write ""
Next
(2) Code for traversing the StaticObjects collection
The StaticObjects collection contains all object references declared using the <OBJECT> element in global.asa. Because each entry is an object variable, the array can be iterated over with simpler code. We will output the name of the object (as originally defined in the ID property):
For Each objItem in Application.StaticObjects
If IsObject(Application.StaticObjects(objItem)) Then
Response.Write "<OBJECT> element: ID='" & objItem & "'
"
End If
Next
2. Operations on collections
1) Add values to the Contents
collection. The method of adding values to the Contents collection is the same as that used in the script code of the global.asa web page. Allows a new Variant value to be added to the Application object, with a suggested name and value (which can be edited as needed), click the button, reload the page, add the value to the Application.Contents collection, and Show in list.
Add the code for the new Contents entry
to place all the buttons and other HTML controls on a form in the sample web page. ACTION sets the path of the current web page and reloads it when the form is submitted. The METHOD attribute is "POST", so the value in the control appears in the Request.Form collection. These two techniques have been used in previous chapters:
<FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST">
The buttons on this form are ordinary HTML INPUT controls , have the same title (three spaces) but a different name. For example, the code to create the first button (add the value to the Application object) is:
<INPUT TYPE="SUBMIT" NAME="cmdAdd" VALUE="">
When reloading the web page, check the Request.Form collection, Determine which SUBMIT button was clicked and handle it accordingly. If it is a button that adds a value to the Application object (the button is named cmdAdd in the HTML <INPUT> element), use the following program segment:
If Len(Request.Form("cmdAdd")) Then
strVarName = Request.Form("txtVarName")
strVarValue = Request.Form("txtVarValue")
Application.Lock
Application("strVarName") = strVarValue
Application.Unlock
End If
Note how the application uses the Application.Lock and Application.Unlock methods to ensure that these values are not confused by two users accessing them concurrently. This is generally not possible if you are just setting a specific value. But it is wise to always use the Lock and Unlock methods.
2) Delete values from the Contents collection.
Create this list when executing the ASP web page by traversing the Contents collection (as we did earlier). However, we only collect the names of each item and put them into the <OPTION> element within the <SELECT> list element:
…
<SELECT NAME=”lstRemove” SIZE=”1”>
<%
For Each objItem in Application.Contents
Response.Write “<OPTION>” & objItem & “</OPTION>”
Next
&>
</SELECT>
…
After the ASP code is executed, the result seen in the browser is:
<SELECT NAME="lstRemove" SIZE="1">
<OPTION>ADOConnection</OPTION>
<OPTION>Variant_Array</OPTION>
<OPTION>Start_Time</OPTION>
<OPTION>Visit_Count</OPTION>
<OPTION>My_New_Value</OPTION>
</SELECT>
(1) Delete a single value
When you click the button to delete a single value, the form is submitted to the same web page again, but this time a SUBMIT button for cmdRemoveThis will be set up, and then the Remove method of the Application.Contents collection will be called. :
If Len(Request.Form("cmdRemoveThis")) Then
strToRemove = Request.Form("lstRemove")
Response.Write "strToRemove = " & strToRemove
Application.Lock
Application.Contents.Remove(strToRemove)
Application.Unlock
End If
Note that this is a method of the Contents collection, not the Application object. The syntax is Application.Contents.Remove, not Application.Remove.
The result of removing the Start_Time value from the Contents collection.
(2) Delete all values.
If you click the last of the three SUBMIT type buttons, the code in the web page will detect that the clicked button is cmdRemoveAll, and will execute the RemoveAll method of the Application.Contents collection:
If Len(Request. Form("cmdRemoveAll")) Then
Application.Lock
Application.Contents.RemoveAll
Application.Unlock
End If
reminds you again that this is a method of the Contents collection, not Application. The syntax is Application.Contents.RemoveAll, not Application.RemoveAll.