The Session Object Maintaining State

298 Indicates the version of the Microsoft XML document object model that the client browser supports. The type is Version defined in the System namespace. Platform Indicates the platform on which the client browser is running. The type is String. For example, for a browser running on Windows 2000 Professional, the Platform property returns WinNT . Tables Indicates whether the client browser supports HTML tables. The type is Boolean. Type Indicates the name and major version number of the client browser e.g., IE6 . The type is String. VBScript Indicates whether the client browser supports VBScript. The type is Boolean. Version Indicates the full version number of the client browser e.g., 8.15 . The type is String. W3CDomVersion Indicates the version of the W3C XML document object model that the client browser supports. The type is Version defined in the System namespace. Win16 Indicates whether the client browser is running on a 16-bit Windows operating system. The type is Boolean. Win32 Indicates whether the client browser is running on a 32-bit Windows operating system. The type is Boolean.

6.8 Maintaining State

The process of serving web pages is inherently stateless. By default, each request to a web server is isolated from every other request that a user might make. This is a fine model for serving static web pages, but its not very useful for full-featured applications. To be useful, applications must keep track of state. This section describes the different options that web developers have for storing state.

6.8.1 The Session Object

When a user browses to a web page served by IIS, IIS considers it the start of a session for that user. A session is an abstract concept that refers to the period of time within which a particular user is interacting with a web application. Although its easy to determine when a session starts, its not so easy to know when that session ends. This is because of the inherent disconnectedness of web browsing. After any given web request is serviced, IIS doesnt really know whether the user will issue more requests browse to more pages. For this reason, IIS establishes a session timeout period. If no 299 new requests are received from a user after a given amount of time, IIS considers that session to have ended. A session corresponds to a single use of a web application by a single user. To assist the web application in maintaining state for the duration of a session, ASP.NET provides an intrinsic object called the Session object. When a session begins, ASP.NET instantiates a Session object. During processing of web requests, the application can store information into the Session object in namevalue pairs. Later requests received during the same session can read the information stored in the Session object. The Session object is of type HttpSessionState defined in the System.Web.SessionState namespace and is available through the Page classs Session property. Here is an example of saving information into a session object: SessionFirstName = txtFirstName.Text For example, this code could be run in response to the user clicking a Submit button on a form. The code assumes that the page has a text box named txtFirstName, in which the user has presumably typed a name. The value thus saved is available until the session ends to all subsequent web requests by the same user. For example, a subsequent page could use this value to personalize a message, like this: Response.WriteHello, SessionFirstName , welcome to ASP.NET The commonly used properties of the HttpSessionState class are: Count The number of items stored in the Session object. The type is Integer. IsNewSession An indication of whether the current request created the session. The type is Boolean. Keys A collection of all keys used in the Session object. A key is the name of the data being stored in the Session object, as opposed to the value of that data. For example, in the following line of code, the key is FirstName : SessionFirstName = txtFirstName.Text The following code fragment loops through all the keys in the Session object: Dim str As String For Each str In Session.Keys ... Next The type of the Keys property is KeysCollection defined within the NameObjectCollectionBase class in the System.Collections.Specialized namespace. StaticObjects A collection of objects declared with the object scope=Session tag in the applications global.asax file. The type is HttpStaticObjectsCollection defined in the System.Web namespace. See Sect ion 6.9 later in this chapter. 300 Timeout The session timeout, in minutes. If a new request isnt received in this amount of time, the session ends. The type is Integer. The default is 20 . Some commonly used methods of the HttpSessionState class are: Abandon Ends the current session. The syntax is: Public Sub Abandon Clear Clears all values from the Session object. The syntax is: Public Sub Clear Remove Clears a single item from the Session object. The syntax is: Public Sub RemoveByVal name As String

6.8.2 The Application Object