SUMMARY
This document illustrates how to use the
Application object to persist data across an Active Server Pages (ASP) application. Items that are stored in the ASP intrinsic
Application object are available to all ASP pages in the current application, as well as across all current sessions.
To implement data persistence, you must have Microsoft Internet Information Server 4.0, Internet Information Server 5.0, or Microsoft Personal Web Server 4.0 or later installed and running.
The example in this article demonstrates how to implement a hit counter for your application. The hit counter calculates the number of users that have logged on to the application since the application was last restarted. The example consists of two pages: Global.asa and Page1.asp. In the Global.asa file, the application variable is set and incremented. The Page1.asp file is used to display the hit counter.
back to the top
Set and Increment the Application Variable in Global.asa
- Create a new Web project in Visual InterDev 6.0. For detailed instructions, see the following article in the Microsoft Knowledge Base:
301184 How To Create a Visual InterDev Project
- In the Project Explorer window, double-click Global.asa to open the Global.asa file.
- Highlight the following code, right-click the code, and then click Copy. In the Global.asa file, click Paste as HTML on the Edit menu to paste the code between the <SCRIPT> tags in Global.asa.
Sub Application_OnStart
Application("NumVisits") = 0
End Sub
Sub Session_OnStart
'This line is only for IIS 4.0. Do not use the Lock
'function in IIS 5.0.
Application.Lock
Application("NumVisits") = Application("NumVisits") + 1
Application("datLastVisited") = Now()
'This line is only for IIS 4.0. Do not use the UnLock
'function in IIS 5.0.
Application.UnLock
End Sub
- Click Save to save the page, and then close the page.
back to the top
Create Page1.asp to Display the Hit Counter
- In the Project Explorer, right-click the project name, click Add, and then click Active Server Page to create a new page named Page1.asp.
- Highlight the following code, right-click the code, and then click Copy. In the Page1.asp file, click Paste as HTML on the Edit menu to paste the code between the <BODY> tags in Page1.asp.
<%
Response.Write "Last Time of Entry: " & Application("datLastVisited") & "<BR>"
Response.Write "Number of Visitors: " & Application("NumVisits") & "<BR>"
%>
- Click Save to save the page.
- In the Project Explorer window, right-click Page1.asp, and then click View in browser to display the page. Notice that the number of visitors appears as 1. This number is only incremented when a new session starts. Close the browser and browse the page again to increment the number of hits.
back to the top
Complete Code Listing
Global.asa:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Application("NumVisits") = 0
End Sub
Sub Session_OnStart
'This line is only for IIS 4.0. Do not use the Lock
'function in IIS 5.0.
Application.Lock
Application("NumVisits") = Application("NumVisits") + 1
Application("datLastVisited") = Now()
'This line is only for IIS 4.0. Do not use the UnLock
'function in IIS 5.0.
Application.UnLock
End Sub
</SCRIPT>
Page1.asp:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
Response.Write "Last Time of Entry: " & Application("datLastVisited") & "<BR>"
Response.Write "Number of Visitors: " & Application("NumVisits") & "<BR>"
%>
</BODY>
</HTML>
back to the top
Troubleshooting
You should not use the
Application object to store user-specific data; only store application-specific data such as database connection strings.
Also, note that you cannot store Single Threaded Apartment objects in Session or Application scope.
back to the top