JSP implicit Session object
by, 11-20-2011 at 05:19 PM (783 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:
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:getAttribute(String name) getAttributeNames isNew() getCreationTime getId invalidate() getLastAccessedTime getMaxInactiveInterval removeAttribute(String name) setAttribute(String, object)
So, as a JSP developer, you may save objects (String, ArrayList, HashList, Vectors etc) into session object and can retrieve whenever you need.Java Code:String userName = (String) session.getAttribute("username");
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.
The above statement returns enumeration of objects, which contains all the unique names stored in the current session object in the enumeration object enumObjects.Java Code:enumObjects = session.getAttributeNames( ) ;