JSP session Object

JSP session Object is implicitly available to the developer on Java Server Pages. Session object is useful in maintaining objects and accessing it values in different JSPs. Session object has a scope of session.

Session Object represents the data associated with a user’s session. The request and response, are used to pass information from web browser to server and from server to web browser respectively. While the connection between client & the server is provided by “session” object. The main use of ‘session’ object is to maintain states during multiple page requests.

The main feature of session object is to navigate between multiple pages in a application where variables are stored for the entire user session. The session objects do not lose the variables and the value remains for the user?s session. The concept of maintenance of sessions can be performed by cookies or URL rewriting.

Methods of session object:

setAttribute(String, object) :

This method sets the value of object as string’s value and you can get this value in other JSP page which is connected to this page.

getAttribute(String name) :

This method is use to fetch the value of “String” which is set by you, in other JSP page, using “setAttribute()” method.

getAttributeNames() :

The “getAttributeNames” method of session object is used to retrieve all attribute names associated with the current session. The name of each object of the current session is returned. The value returned by this method is an enumeration of objects that contains all the unique names stored in the session object.

isNew():

The isNew() method of session object returns a true value if the session is new. If the session is not new, then a false value is returned. The session is marked as new if the server has created the session, but the client has not yet acknowledged the session. If a client has not yet chosen the session, i.e., the client switched off the cookie by choice, then the session is considered new. Then the isNew() method returns true value until the client joins the session. Thus, the isNew() method session object returns a Boolean value of true of false.

invalidate():

This method invalidates this session and unbinds any objects bound to it.
Check whether session object is null or not. If the session object null means, session is already expired or not yet initiated. Use invalidate() method on session object to invalidate a session if the session object is not null.

setMaxInactiveInterval(int interval):

Get max time in milliseconds of inactive interval. This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

getMaxInactiveInterval():

This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

removeAttribute(String name):

Remove attribute of given name from session object.

public long getLastAccessedTime():

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

public String getId():

This method returns a string containing the unique identifier assigned to this session.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next