Developing Web Applications

Previous Topic Next Topic

ASP Session Management

Since HTTP is a stateless protocol, the Web server retains no memory of past actions and treats each browser request as a one-time event. This statelessness makes it difficult for Web developers to create the level of application interactivity that most users expect. Although persistent application state can be maintained in a database or stored in files, such solutions are often difficult to implement, and involve a hefty memory overhead and performance penalty. ASP surmounts these problems by providing its own session management.

Using the ASP Session object, one of the ASP built-in objects, developers can store any data they wish, including references to objects they have instantiated. The Session object is analogous to an associative array, into which values may be stored and retrieved by using a string (or keyword) as the index.

For example, the following instruction assigns “John Doe” to the MyName entry in the array:

<% Session("MyName") = "John Doe" %>

The value “John Doe” is retrieved from the Session object by using the MyName keyword, as shown here:

My name is: <%= Session("MyName") %>

The Session object exists in memory on the server and maintains its state for the duration of the user’s Web session. You can disable this object on a page-by-page basis using the declarative <%@ EnableSessionState=False %>. Disabling the Session object does not affect values that have previously been stored in this object. However, it may speed up the processing of pages that don’t use the Session object. Furthermore, multiple ASP pages in an HTML frameset may be serialized (run in sequential order) if they use session state. Disabling the Session object in child frames that don’t require it will allow the frame requests to execute concurrently.

The Application object is used in a similar fashion to store values that persist for the lifetime of the ASP application; it is also used to share values across user sessions. The values stored in the Session and Application objects persist between page requests, and can be retrieved at any point using the keyword that they were assigned.

See the following:


© 1997-1999 Microsoft Corporation. All rights reserved.