Open In App

JSP Session – Implicit Object

Last Updated : 30 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Implementation:

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

HTML




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
 
</body>
</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

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
 
session.setAttribute("user",name);
%>
 
<a href="second.jsp">Display the value</a>
 
</body
</html>


In second.jsp page, the value of variable is retrieved from the session and displayed. 

Example 2(B) second.jsp

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<h1>Display the session value on this page</h1>
 
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
 
</body>
</html>


Outputs: They are sequentially as follows:  

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads