In this tutorial, you will learn session implicit object in JSP. JSP session implicit object is the most frequently used implicit object in JSP. The main use of session is to gain access to all the user’s data till the user session is active.
Methods of session Implicit Object
- setAttribute(String, object): This method is used to save an object in session by assigning a unique string to the object. Later, the object can be accessed from the session by using the same String till the session is active. setAttribute and getAttribute are the two most frequently used methods while dealing with session in JSP.
- getAttribute(String name): The object stored by setAttribute method is fetched from session using getAttribute method. For example if we need to access userid on every JSP page till the session is active then we should store the user-id in session using setAttribute method and can be accessed using getAttribute method whenever needed.
- removeAttribute(String name): The objects which are stored in session can be removed from session using this method. Pass the unique string identifier as removeAttribute’s method.
- getAttributeNames: It returns all the objects stored in session. Basically, it results in an enumeration of objects.
- getCreationTime(): This method returns the session creation time, the time when session got initiated (became active).
- getId(): Servlet container assigns a unique string identifier to session while creation of it. getId method returns that unique string identifier.
- isNew(): Used to check whether the session is new. It returns Boolean value (true or false). Mostly used to track whether the cookies are enabled on client side. If cookies are disabled the session.isNew() method would always return true.
- invalidate(): It kills a session and breaks the association of session with all the stored objects.
- getMaxInactiveInterval: Returns session’s maximum inactivate time interval in seconds.
- getLastAccessedTime: Generally used to know the last accessed time of a session.
JSP session Implicit Object Example
The following html page would display a text box along with a submit button. The submit action transfers the control to another JSP page
<html> <head> <title>Welcome Page: Enter your name</title> </head> <body> <form action="session.jsp"> <input type="text" name="inputname"> <input type="submit" value="click here!!"><br/> </form> </body> </html>
The session.jsp page displays the name which user has entered in the index page and it stores the the same variable in the session object so that it can be fetched on any page until the session remains active.
<html> <head> <title>Passing the input value to a session variable</title> </head> <body> <% String uname=request.getParameter("inputname"); out.print("Welcome "+ uname); session.setAttribute("sessname",uname); %> <a href="output.jsp">Check Output Page Here </a> </body> </html>
In this page we are fetching the value of name variable, which has been stored in the session using
setAttribute() method of session implicit object. The user is still active so the user data stored in the session can be accessed. Here, we accessed the user name using
<html> <head> <title>Output page: Fetching the value from session</title> </head> <body> <% String name=(String)session.getAttribute("sessname"); out.print("Hello User: You have entered the name: "+name); %> </body> </html>