<!--session declaration makes the session implicit
variable available to a JSP page.-->
<%@page import = "java.util.*" session="true"%>
<HTML>
<HEAD>
<TITLE>Using Sessions to Track Users
through JSP Code</TITLE>
</HEAD>
<BODY bgcolor="#084B8A">
<font size="+3" color="#E6E6E6"><br>track
user using session</font>
<TABLE style="background-color: #CEF6F5;" border="1">
<%
/* here if counter has not been set before,
getAttribute will return a value of null. That means
you can create the counter value, or increment it if
it already exists, and store the new value in the
session object*/
Integer counter = (Integer)session.getAttribute("counter");
if (counter == null) {
counter = 1;
}
else {
counter = counter+1;
}
/* setAttribute() binds counter value to this session,
using the specified name here "counte".*/
session.setAttribute("counter", counter);
%>
<!-- use getId() method to get a unique session id -->
<tr><th>Session ID</th><td><%=session.getId()%></td></tr>
<!-- getCreationTime() method returns date and time
when session was created -->
<tr><th>Session creation time</th><td><%=new
Date(session.getCreationTime())%></td></tr>
<!--getlastAcccessTime() method returns date and time of
last access by this session id-->
<tr><th>Last accessed time</th><td><%=new
Date(session.getLastAccessedTime())%></td></tr>
<!-- this counter variable will print how much time user
visited in application -->
<tr><th>visited</th><td><%=counter%> times</td></tr>
</BODY>
</HTML>
|