Accessing Time

Accessing Time

Posted in : Java Posted on : July 6, 2011 at 7:51 PM Comments : [ 0 ]

In this section we will get how we can find the accessing time with its session Id.

Accessing Time

In this section we will get how we can find the accessing time with its session Id.

In the example given below I used the methods of HttpSession. These methods are getId(), getCreationTime(), and lastAccessedTime(). I have wrapped the ssn.getCreationTime() and ssn.lastAccessedTime() method into the Date because of the date and time will be showed in simple format.

Example :

import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LastAccessedSession extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
HttpSession ssn = req.getSession(true);
PrintWriter pw = res.getWriter();
String s = ssn.getId();
pw.println("Your Session Id = "+ s);
pw.println("<br>Your Session creation time = "+ new Date(ssn.getCreationTime()));
pw.println("<br>You had accessed the session last time = "+ new Date(ssn.getLastAccessedTime()));


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics