View RSS Feed

My Java Tips

JSP implicit Session object

Rate this Entry
by , 11-20-2011 at 06:19 PM (1939 Views)
Session Object is another implicit JSP object that denotes the data associated with a specific session of a user. It is associated to Javax.servlet.http.httpsession.

The connection between client and the server is provided by the session object. It maintains states when there are multiple page requests. If the user is navigating between multiple pages, and you need to store some data associated with that user, then session object helps you in this.

Some useful session object methods are:

Java Code:
getAttribute(String name)
getAttributeNames isNew()
getId invalidate()
removeAttribute(String name)
setAttribute(String, object)
The getAttribute method of session object is used to return the object (java.lang.Object) with the specified name given in parameter. If there is no object then a null value is returned.


Java Code:
String userName = (String) session.getAttribute("username");
So, as a JSP developer, you may save objects (String, ArrayList, HashList, Vectors etc) into session object and can retrieve whenever you need.

getAttributeNames method of session object is used to get all the stored session objects (current session). It returns an enumeration object and we can iterate through it to deal wit all the session objects.


Java Code:
enumObjects = session.getAttributeNames( ) ;
The above statement returns enumeration of objects, which contains all the unique names stored in the current session object in the enumeration object enumObjects.

Submit "JSP implicit Session object" to Facebook Submit "JSP implicit Session object" to Digg Submit "JSP implicit Session object" to Submit "JSP implicit Session object" to StumbleUpon Submit "JSP implicit Session object" to Google