Advertisement



< Prev
Next >



JSP session Object





JSP's inbuilt session object is of type HttpSession interface and it is implicitly available in the JSP code. The session attribute of page directive is bound to the session object. The value of session attribute indicates whether or not the JSP page uses HTTP sessions i.e. if we can access the session object within a JSP page.

By default, JSP sessions are automatically created because the session attribute of page directive is set to true, by default. But, we can manually turn-off the automatic creation of JSP sessions by setting the session attribute of page directive to false and in that case, accessing the 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 set to true, hence we can easily access the information about a JSP session by using the JSP's implicit session object, and through it accessing the methods of HttpSession. Let's see how.


Session.jsp
<html>

<head>
<title>Creating a Session</title>
</head>

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

 
   


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

</body>

<html>



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




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


Advertisement




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


By default, the session attribute of the page directive is automatically set to true, but after manually setting it to false, we will get an error when accessing the JSP implicit session object. Let's take a look in the next example.


Session2.jsp
<html>

<head>
<title>Creating a Session</title>
</head>

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

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/>

</body>

<html>



Executing this JSP page displays an error, because we are accessing the JSP's inbuilt session object after setting the session attribute to false, within page directive.






Please share this article -




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



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement