< Prev
Next >

JSP session Object

JSP inbuilt session object is of type HttpSession interface and it is implicitly available in the JSP code. By default, JSP Sessions are created automatically because session attribute of page directive is set to true by default. Hence, unless, we set session attribute of page directive to false(which turns off the session) and in that case, accessing a session object leads to an error.

Some useful methods available to session object.

The methods in the table below are part of javax.servlet.http.HttpSession interface. Hence, these methods are available to the JSP implicit session object, which is a type of HttpSession.

Methods Description
void setAttribute(String name, Object value) This method sets an object with a name in a session.
Object getAttribute(String name This method gets an object stored in a session with a name, or null.
void removeAttribute(String name) This method removes an object with a name from the session.
long getLastAccessedTime() This method returns the last time(since midnight Jan 1, 1970 in milliseconds) since a client accessed this session.
int getMaxInactiveInterval() This method gets the time(in seconds) before servlet contained invalidates the session.
void setMaxInactiveInterval(int seconds This method sets the time between client requests before the servlet container invalidates the session.
long getCreationTime() This method gets the creation time of the current session, since 1970 in milliseconds.
ServletContext getServletContext() This method returns the ServletContext to the current session.
boolean isNew() This method gives a true if the current session is new and client doesn't know about it.

Using JSP session object to access information about a session.

By default, the session attribute within the page directive is always "true", hence we can easily access the information about a JSP session using the JSP implicit session object and through it accessing the methods of HttpSession. Let's see how.


<title>Creating a Session</title>

<%@page import="java.util.Date" session="false"%>


Information about the newly created session by this JSP web page - <br/>
Is Session new : <%= session.isNew() %>
Session ID: <%= session.getId()%> <br/>
Session creation time: <%= new Date(session.getCreationTime())%> <br/>
Last Accessed Time of Session : <%= session.getLastAccessedTime()%>  milliseconds since Jan 1, 1970.   <br/>
Maximum inactive interval : <%= session.getMaxInactiveInterval()%> seconds



executing this JSP page displays the information about the newly created JSP session using JSP's implicit session object.

As you can see in the output, isNew() method which tells us about if the session is new or not, returned true because this JSP session is newly created and accessed for the first time but from second time you access the same page in the session, isNew() method will return false.

Setting session attribute to "false" in JSP page directive.

By default, the session attribute within the page directive is always true but on setting it to false we get an error when accessing the JSP implicit session object. Let's take a look in the next example.


<title>Creating a Session</title>

<%@page import="java.util.Date" session="false"%>

No session will be created after setting session attribute to false in page directive.
Hence, accessing the session object  will result in an error.

Session ID: <%= session.getId() %>  <br/>
Session creating time : <%= new Date(session.getCreationTime()) %>  <br/>



executing this JSP page displays error when accessing the JSP's inbuilt session object after setting the session attribute to false within page directive.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< JSP out Action
JSP application object >

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page