Article ID: 300883 - View products that this article applies to.
This article was previously published under Q300883
This step-by-step procedure demonstrates how to set and retrieve information from both Session and Application objects in an Active Server Pages (ASP) application.
Session and Application variables basicsYou can use the Application and Session objects to store values that are global rather than page-specific for either a particular user (the Session) or to all users (the Application).
The Session and Application variables are stored on the server. Client browsers are then attached to the session through a cookie. As a result, the client must have cookies enabled in the browser for Session and Application variables to work.
The samples to follow demonstrate how to use Visual InterDev to create Session and Application variables.
Create a Visual InterDev Web project
How to use Session variablesThe power of the Session object comes from the fact that it can store variables that are global to just that specific user; as a result, each user can have their own individual value for that variable. Session objects are not always created automatically for every user when they enter your application. However, storing or accessing a variable in the Session object creates the Session object and fires the Session_OnStart event.
To demonstrate how to use the Session object in an ASP page, follow these steps:
How to use Application variablesThink of the Application object as a global container for information that is available to all pages of your ASP application. You can store variables and object references in the Application object. The Application object is instantiated when the first page of your application is requested and remains available until the Web service is shut down.
To demonstrate how to use the Application object in an ASP page, follow these steps:
Application_OnStart and Session_OnStart eventsYou can use both the Application_OnStart and Session_OnStart events to initialize state. To do this, set up variables that are global for either the application or a specific user. When the first user accesses a file in your application, the Application_OnStart event is triggered. This event is used to initialize any application-wide global variables. When the user begins a session for the first time, the Session_OnStart event is triggered. This event is used to initialize user-specific information.
Application_OnEnd and Session_OnEnd eventsThe Session_OnEnd event occurs either when a current session times out. By default, this is 20 minutes after the last request for a page from the application. To change this default time out setting, you can set the Session.Timeout property or edit the registry.
You can use the Application_OnEnd event to clean up all of the global objects and variables. However, in a present problem, this event may not be triggered until the Web server is stopped. Revisions of ASP seem likely to specify that the Application_OnEnd event be triggered once the last Session_OnEnd event occurs, that is, when the last session ends and there are no current application users.
TroubleshootingApplication and Session variables require that clients have cookies enabled in their Web browser, which is not reliable between shared frames in a frameset or across multiple browser instances.
Session and Application events are only triggered when a client retrieves an ASP page; they are not triggered when an HTML page in the application is requested. Therefore, if you have additional server-side applications such as Internet Server Application Programming Interface (ISAPI) or Common Gateway Interface (CGI) scripts, make sure that these applications do not depend on specific events having occurred within an ASP page. Otherwise, the ISAPI or CGI script may fail (crash) and cause the Web server to stop responding (hang).
This method stores information on the Web server. This decreases scalability in two ways: Session and Application variables use resources on the server and cannot be used on completely load balanced Web Farms.
For more information, see the following Microsoft Web sites:
Avoiding traps when using the Application and Session objectsFor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
Using variables and constants
Active Server Pages guide
ASP scripting optimization
(http://support.microsoft.com/kb/281298/ )PRB: Session variables do not persist when application contains empty Global.asa
(http://support.microsoft.com/kb/230149/ )How to access Session and Application variables from within a Visual Basic component