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.io.IOException;
import java.io.PrintWriter;
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
{
res.setContentType("text/html");
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()));
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>accessingtime</display-name>
<servlet>
<servlet-name>LastAccessedSession</servlet-name>
<servlet-class>LastAccessedSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LastAccessedSession</servlet-name>
<url-pattern>/LastAccessedSession</url-pattern>
</servlet-mapping>
</web-app>

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics