SUMMARY
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.
back to the top
Session and Application variables basics
You 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.
back to the top
Create a Visual InterDev Web project
- Create a new Web Project in Visual InterDev. For detailed instructions, see the following article in the Microsoft Knowledge Base:
301184 How to create a Visual InterDev project
- From the Project menu, point to Add Web Item, and then click Active Server Page.
- Click Open to accept the default information for the new ASP page.
Now that you added an ASP page to the current project, the ASP code samples in the next two sections demonstrate the use of both
Session and
Application variables.
back to the top
How to use Session variables
The 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:
- Paste the following code between the <BODY> </BODY> tags of the ASP page that you created earlier in Visual InterDev:
<%
'Store information in a Session variable.
Session("myInformation") = "somevalue"
'Display the contents of the Session variable.
Response.Write Session("myInformation")
%>
- Click View in Browser from the View menu.
- When you are prompted to save the file, click OK. The browser displays the information in the variable.
back to the top
How to use Application variables
Think 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:
- Paste the following code between the <BODY> </BODY> tags of the ASP page that you created earlier in Visual InterDev:
<%
'Store information in an Application variable.
Application("myvalue") = "something"
'Display the contents of the Application variable.
Response.Write Application("myvalue")
%>
- Click View in Browser from the View menu.
- When you are prompted to save the file, click OK. The browser displays the information in the variable.
back to the top
Application_OnStart and Session_OnStart events
You 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.
back to the top
Application_OnEnd and Session_OnEnd events
The
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.
back to the top
Troubleshooting
Application 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.
back to the top