The following example code demonstrates how to write a Program in JSP to Display Session Information.
Here is an example code for a JSP program that displays session information.
<%
HttpSession session = request.getSession();
Integer count = (Integer) session.getAttribute("count");
if (count == null) {
count = 1;
} else {
count++;
}
session.setAttribute("count", count);
%>
<h1>Session Information</h1>
<p>Session ID: <%= session.getId() %></p>
<p>Creation Time: <%= new java.util.Date(session.getCreationTime()) %></p>
<p>Last Access Time: <%= new java.util.Date(session.getLastAccessedTime()) %></p>
<p>Visit Count: <%= count %></p>
In this code, the request.getSession()
method is used to retrieve the current session. If a session does not exist, a new one is created. The code then increments a count attribute stored in the session. The session ID, creation time, last access time, and visit count are then displayed on the page.