3.2 Session Object

Session object stores information such as preferences for a specific user-session. Variables stored in this object are not deleted when the user jumps between pages in the application. Instead, these variables are available for the entire user session.

NSP automatically creates a Session object when a Web page from the application is requested by a user who does not already have a session. The server destroys the Session object when the session expires or is abandoned.

3.2.1 Contents Property

Returns the contents object corresponding to the specified key value.

Syntax

Session.Contents(Key)

Type

Contents object.

Attributes

Read-only.

Example

See sample in Example.

3.2.2 SessionID property

Returns the unique session identifier that is generated when a session is created.

Syntax

Session.SessionID

Type

String.

Attributes

Read-only.

Example

See sample in Example.

3.2.3 TimeOut property

Sets or returns timeout period (in minutes) assigned to the Session object.

Syntax

Session.TimeOut

Type

Long.

Attributes

Read/write.

Remarks

If the user does not refresh or request a page within the specified timeout period then, the session ends. By default the time out period is set to 10 mins.

Example

See sample in Example.

3.2.4 Abandon method

Destroys all the variables stored in a Session object and releases their resources.

Syntax

Session.Abandon()

Parameters

None.

Return Values

Boolean.

Remarks

If this method is not explicitly called, NSP destroys all the variables associated with the session object when that session times out. But the session variable will be used in that script.

Example

See sample in Example.

3.2.5 GetValue method

Retrieves the value of the session variable Key.

Syntax

Session.GetValue(
   Key As String)

Parameters

Key

Corresponds to the name of a session variable.

Return Values

String.

Remarks

To use this method, the session variable must have been previously set in the Session object.

Example

See sample in Example.

3.2.6 RemoveAll method

Deletes all session variables that have been added to the Session object.

Syntax

Session.RemoveAll()

Parameters

None.

Return Values

Boolean.

Example

This example creates a session with variables "book name" and "price" and displays the details of all the books. Then, it removes all the variables.

<% ’Sets the value for the keys username and company
Session.Setvalue("Book Name","Beauty and the Beast" )
Session.Setvalue("Price","10 USD")  %>
    
<% ’Returns the session ID
Response.write ("The session ID is "&session.sessionID &
"<br>"%>
    
<% ’Returns the details of the book
Response.write ("The price of  "&Session.getvalue("Book
Name") &" is" &session.getvalue("price") & "<br>"%>
    
<% ’Removes the specifies key from the contents collection and
’returns the result
If Session.Removeall() Then
Response.write ("deleted successfully")
Else
Response.write ("unable to delete")
End If %>

3.2.7 RemoveValue method

Deletes a specific item from the Session object.

Syntax

Session.RemoveValue(
   Key As String)

Parameters

Key

Name of the session variable.

Return Values

Boolean.

3.2.8 SetValue method

Sets the value of the session variable Key.

Syntax

Session.SetValue(
   Key As String, 
   Value As String)

Parameters

Key

String. Name of the session variable.

Value

String. Value of the session variable.

Return Values

Boolean.

Remarks

If the session variable is already present, the existing value will be overwritten.

Example

This example creates a session with key values "username" and "company" and sets the value for them. This also returns the session ID, timeout period and company name of the specific user and abandons the session.

<% ’Sets the value for the keys username and company Session.Setvalue("username","Sudha" )  Session.Setvalue("Company","Novell")  %> 

<% ’Returns the company name for each user in the user ’collection 
For each username in session.contents 
Response.write ("The company is "&Session.getvalue("company") &"<br>")
Next  %>

<% ’Returns the session ID
Response.write ("The session ID is "&session.sessionID
&"<br>"%>

<% ’Returns the timeout period
Response.write ("The timeout period is "&session.timeout
&"<br>"%>

<% ’Abandons the session and returns the result
If Session.Abandon() Then
Response.write ("The session is abandoned")
Else
Response.write ("The session is not abandoned")
End If %>