JSP Session – Implicit Object
In JSP, the session is the most regularly used implicit object of type HttpSession. It is mainly used to approach all data of the user until the user session is active.
Methods used in session Implicit Object are as follows:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Method 1: isNew(): This method is used to check either the session is new or not. Boolean value (true or false) is returned by it. Primarily it used to trace either the cookies are enabled on client side or not. If cookies are not enabled then the session.isNew() method should return true all the time.
Method 2: getId(): While creating a session, the servlet container allots a distinctive string identifier to the session. This distinctive string identifier is returned by getId method.
Method 3: getAttributeNames(): All the objects stored in the session are returned by getAttributeNames method. Fundamentally, this method results in an enumeration of objects.
Method 4: getCreationTime(): The session creation time (the time when the session became active or the session began) is returned by getCreationTime method.
Method 5: getAttribute(String name): Using getAttribute method, the object which is stored by the setAttribute() method is retrieved from the session. For example, We need to store the “userid” in session using the setAttribute() method if there is the requirement of accessing userid on every jsp page until the session is active and when needed it can be accessed using the getAttribute() method.
Method 6: setAttribute(String, object): The setAttribute method is used to store an object in session by allotting a unique string to the object. Later, By using the same string this object can be accessed from the session until the session is active. In JSP, while dealing with session setAttribute() and getAttribute() are the two most regularly used methods.
Method 7: getMaxInactiveInterval(): The getMaxInactiveInterval return session’s maximum inactivates time interval in seconds.
Method 8: getLastAccessedTime: The getLastAccessedTime method is mostly used to notice the last accessed time of a session.
Method 9: removeAttribute(String name): Using removeAttribute(String name) method, the objects that are stored in the session can be removed from the session.
Method 10: invalidate(): The invalidate() method ends a session and breaks the connection of the session with all the stored objects.
The ‘index.html’ page given below will display a text box together with a go button. On clicking go button, the control is transferred to welcome.jsp page. All the outputs are appended at last.
Example 1: index.html
The name which is entered by user in the index page is displayed on the welcome.jsp page and it saves the same variable in the session object so that it can be retrieved on any page till the session becomes inactive.
Example 2(A): welcome.jsp
In second.jsp page, the value of variable is retrieved from the session and displayed.
Example 2(B) second.jsp
Outputs: They are sequentially as follows: