Session Tracking Servlet Example

In this tutorial you will learn how to track session in servlet.

Session Tracking Servlet Example

In this tutorial you will learn how to track session in servlet.

Session Tracking Servlet Example

Session Tracking Servlet Example

In this tutorial you will learn how to track session in servlet.

Session tracking is required for identifying the client which was interacting with the server and sat idle for some time is the same client or the other when it tries to interact next time to the server. Session tracking can be done by various of methods such as by storing an identifier information on the client side as a Cookie, including the identifier in every URL which will returned to the client, by url rewritting encodeURL(URL), using session object. Here I am using the session object.

Here I am giving the simple example in which I have created a HttpSession object using which get the session creation time, last accessed time, maximum inactive interval. session id, and set the maximum inactive interval, attribute and making request for the URL & URI using HttpServletRequest object.

Example :

SessionManagementExample.java

package roseindia.webContext;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/SessionManagementExample")
public class SessionManagementExample extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
Date creationTime= new Date(session.getCreationTime());
Date lastAccessedTime= new Date(session.getLastAccessedTime());
session.setMaxInactiveInterval(1000);
Integer count;
count = (Integer)session.getAttribute("Count");
if (count == null)
{
count = 0; 
}
else
{
count = new Integer(count + 1);
}
session.setAttribute("Count", count);
try
{
out.println("<h2>Sevlet Session Example</h2>");
if(count==0 || count==1)
{
out.println("<b>In current session this site is accessed " + count + " time.</b>");
}
else
out.println("<b>In current session this site is accessed "+ count + "times. </b>");
out.println("<br>Session ID = (" + session.getId() + ")</br>");
out.println("<br>Session creation time = ("+creationTime+")");
out.println("<br>Session last accessed time ("+lastAccessedTime+")");
out.println("<br>Max inactive interval of session is "+session.getMaxInactiveInterval());
out.println("<br>The complete url = "+request.getRequestURL());
out.println("<br>Part of this url = "+request.getRequestURI());
}
catch(Exception ex)
{
out.println(ex);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request,response);
}
}

Output :

When you will execute the above example you will get the output as :

Download Source Code