Working with JSP Sessions
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.
General syntax of getCreationTime of session object is as follows:
|
The above method returns value of time in milliseconds.
For example:
|
The above statement would display
|
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.
General syntax of getLastAccessedTime of session object is as follows:
|
The above method returns the value of time in milliseconds.
For example:
|
The above statement would display:
|
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.
General syntax of getID of session object is as follows:
|
For example:
|
The above statement would display
|
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.
General syntax of invalidate of session object is as follows:
|
getMaxInactiveInterval:
The getMaxInactiveInterval method of session object is used to return the maximum amount of time the JRun keeps the session open between client accesses. This returns the maximum amount of time, in seconds, that a session can be inactive before it is deleted. The returned value of time is in seconds. Thus, by using this method the user determines how long it will take a session for it to time out. The default timeout period for sessions defined by the servlet container is determined using the getMaxInactiveInterval method. The returned value from this method is seconds and thus, an integer.
General syntax of getMaxInactiveInterval of session object is as follows:
|
The above method returns value of time in seconds.
For example:
|
The above statement would display
|
The above denotes the time in seconds.
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.
General syntax of setMaxInactiveInterval of session object is as follows:
|
In the above the time given in parameter is in seconds.
For example:
|
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.
General syntax of removeAttribute of session object is as follows:
|
For example:
|
Example exforsys given in parameter of removeAttribute method is a string.
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.
General syntax of setAttribute of session object is as follows:
|
For example:
|
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.