The Session Object

The session object provides a way to track users visiting a web site, and to store information about them. Once the session has been created it persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times.               

Shown below is the session object prototype chain.

The request object is of class Session.

Using the Session Object

In order to use the session object a session must be created; this is done by using a global method – createSession(); if a session already exists this method will have no effect.

e.g. index.html (user’s first point of entry into web application)

<html>

  <script runat="server">

    createSession();

  </script>

  //body follows….

The session object is a property of the request object, named jsession, and can be accessed in the following manner:

var session = request.jsession;

Descriptions of the properties and methods associated with the session object follow.

Session Object Properties

All predefined properties are read-only and can not be changed by the script.

Property

Description

creationTime

The time when the session was created, measured in milliseconds since midnight January 1, 1970 GMT.

id

The unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

isNew

Equals true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

DisplayType

Whether the session displays via Green Screen, Windows Client, PROIV Open Client, or does not display (i.e. in batch mode).

Session Object Functions

Function

Description

getLastAccessedTime()

Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

Actions that your application takes, such as getting or setting a value associated with the session, do not affect the access time.

setMaxInactiveInterval(

interval)

Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.

getMaxInactiveInterval()

Returns the maximum time interval in seconds that the servlet container will keep this session open between client accesses. After this interval, the servlet container will invalidate the session. The maximum time interval can be set with the setMaxInactiveInterval method. A negative time indicates the session should never timeout.

getAttribute(name)

Returns the object with the specified name in this session

getAttributeNames()

Returns an array containing the names of all the objects bound to this session

setAttribute(name, object)

Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.

removeAttribute(name)

Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

invalidate()

Causes the  session to become invalidate with respect to the container.

Comment on this topic

Topic ID: 150109