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.
Returns the contents object corresponding to the specified key value.
Session.Contents(Key)
Contents object.
Read-only.
See sample in Example.
Returns the unique session identifier that is generated when a session is created.
Session.SessionID
String.
Read-only.
See sample in Example.
Sets or returns timeout period (in minutes) assigned to the Session object.
Session.TimeOut
Long.
Read/write.
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.
See sample in Example.
Destroys all the variables stored in a Session object and releases their resources.
Session.Abandon()
None.
Boolean.
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.
See sample in Example.
Retrieves the value of the session variable Key.
Session.GetValue(
Key As String)
Corresponds to the name of a session variable.
String.
To use this method, the session variable must have been previously set in the Session object.
See sample in Example.
Deletes all session variables that have been added to the Session object.
Session.RemoveAll()
None.
Boolean.
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 %>
Deletes a specific item from the Session object.
Session.RemoveValue(
Key As String)
Name of the session variable.
Boolean.
Sets the value of the session variable Key.
Session.SetValue(
Key As String,
Value As String)
String. Name of the session variable.
String. Value of the session variable.
Boolean.
If the session variable is already present, the existing value will be overwritten.
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 %>