Академический Документы
Профессиональный Документы
Культура Документы
html
In this JSP tutorial, you will learn about JSP session object methods, getCreationTime,
getLastAccessedTime, getId, invalidate(), getMaxInactiveInterval, setMaxInactiveInterval(),
removeAttribute(String name) and setAttribute(String, object).
This section details the syntax, usage, example and explanation of more session object
methods, such as:
• getCreationTime
• getLastAccessedTime
• getId
• invalidate()
• getMaxInactiveInterval
• setMaxInactiveInterval()
• removeAttribute(String name)
• setAttribute(String, object)
getCreationTime:
The getCreationTime method of session object is used to return the session created time. The
returned time value would be in milliseconds, the time value is midnight January 1, 1970
GMT.
session.getCreationTime()
For example:
The above output depicts the creation time of session since January 1, 1970 GMT in
milliseconds.
getLastAccessedTime:
The getLastAccessedTime method of session object is used to return the latest time of the
client request associated with the session. By using this method, it is possible to determine the
last time the session was accessed before the current request. The returned time value would be
in milliseconds and the time value is since midnight January 1, 1970 GMT.
Page No. 1 of 4
JB’s Collection http://www.exforsys.com/tutorials/jsp/jsp-session-object-methods.html
session.getLastAccessedTime()
For example:
The above output depicts the last time the session was accessed before the current request since
January 1, 1970 GMT in milliseconds.
getId:
The getID method of session object is used to return the unique identifier associated with the
session.
session.getId()
For example:
The above statement denotes the unique identifier associated with the current session.
invalidate():
invalidate method of session object is used to discard the session and releases any objects
stored as attributes. This method helps to reduce memory overhead and achieves improvement
in performance. It is always a good practice to explicitly remove or invalidate sessions using
session.invalidate() method.
Page No. 2 of 4
JB’s Collection http://www.exforsys.com/tutorials/jsp/jsp-session-object-methods.html
session.invalidate()
getMaxInactiveInterval:
session.getMaxInactiveInterval()
For example:
setMaxInactiveInterval():
This is another setMaxInactiveInterval() method that a developer can use to set the
timeout explicitly for each session. A user can use this method to set the default timeout.
session.setMaxInactiveInterval(time)
For example:
Page No. 3 of 4
JB’s Collection http://www.exforsys.com/tutorials/jsp/jsp-session-object-methods.html
session.setMaxInactiveInterval(600);
In the above statement, the inactivity period for the session would be set to 10minutes.The
parameter 600 given in the method would be in seconds.
removeAttribute(String name):
The removeAttribute method of session object is used to remove the attribute and value from
the session.
session.removeAttribute(String)
For example:
session.removeAttribute("exforsys");
setAttribute(String, object):
The setAttribute method of session object is used to set the object to the named attribute.
This method is used to write an attribute and value to the session. If the attribute does not exist,
then it is created and then the object is associated with this.
session.setAttribute(String, object);
String exforsys = request.getParameter("test");
session.setAttribute("test", exforsys);
In the above example, the first parameter passed to the method setAttribute test denotes a
string and the second parameter exforsys denotes the object. By passing these to the
setattribute method, the object exforsys is set with the string test.
Page No. 4 of 4