Use Session to Track User in JSP

ession tracking is a mechanism that is used to maintain state about a series of requests from the same user(that is, requests originating from the same browser) across some period of time.

Use Session to Track User in JSP

Use Session to Track User in JSP

     

This is detailed java code to track user in application by using session.

Session Tracking:  Session tracking is a mechanism that is used to maintain state about a series of requests from the same user(that is, requests originating from the same browser) across some period of time. A session id is a unique token number assigned to a specific user for the duration of that user's session.

Methods to work with session :

1. Cookies
2. URL Rewriting
3. Hidden Fields
4. Session API

This example explains creating session and using session information with the help of session API. JSP provides an implicit object called session which can be used to work with session. You can set and get attributes to the session with the help of setAttribute() and getAttribute() methods respectively. Other session information (session id, session creation time, session last accessed time etc.) can also found using appropriate methods. We have created session_track_jsp.jsp page which will help you to understand how to work with session.

session_track_jsp.jsp

<!--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>

Save this code as a .jsp file named "session_track_jsp.jsp" in your application directory ('user' in our example) in Tomcat and run this jsp page with url http://localhost:8080/user/session_track_jsp.jsp in address bar of the browser.

When user refresh this page then page will reload and visited column shows that page is accessed one more time.

Download Source Code