JSP Session - Implicit Object

Last Updated : 16 May, 2026

JSP session is an implicit object of type HttpSession used to store and retrieve user data across multiple requests. It remains active until the session expires or is invalidated, allowing applications to maintain user state.

  • Available by default in JSP (no need to declare)
  • Used for login, user tracking, and session management

Common Methods of Session Object

  • isNew(): Checks if session is new (returns true/false)
  • getId(): Returns unique session ID
  • getAttribute(String name): Retrieves stored value
  • setAttribute(String, Object): Stores value in session
  • removeAttribute(String name): Removes stored value
  • invalidate(): Ends the session
  • getCreationTime(): Returns session start time
  • getLastAccessedTime(): Returns last accessed time

Step-by-Step Implementation of Implicit Object

Step 1: Create index.html

  • Create an HTML form to take user input
  • On submit, data is sent to welcome.jsp

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.

Step 2: Create welcome.jsp

  • Retrieve user input using request.getParameter()
  • Display the name
  • Store it in session using session.setAttribute()

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>

Step 3: Create second.jsp

  • Retrieve stored session data
  • Display it on another page

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:  

Advantages of Session

  • Maintains user data across pages
  • No need to send data repeatedly
  • Easy to use with built-in methods

Limitations

  • Uses server memory
  • Session expires after inactivity
  • Less secure if not managed properly
Comment