Servlet: Session display : Session « Servlets « Java






Servlet: Session display

  
import java.text.DateFormat;
import java.util.Date;

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

public class SessionDisplay extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    Date creationTime = new Date(session.getCreationTime());
    Date lastAccessed = new Date(session.getLastAccessedTime());
    Date now = new Date();
    DateFormat formatter = DateFormat.getDateTimeInstance(
        DateFormat.MEDIUM, DateFormat.MEDIUM);

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Displaying the Session Creation and Last-Accessed Time</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h2>Session Creation and Last-Accessed Time</h2>");
    out.println("The time and date now is: " + formatter.format(now)
        + "<br><br>");
    out.println("The session creation time: HttpSession.getCreationTime( ): "
            + formatter.format(creationTime) + "<br><br>");
    out.println("The last time the session was accessed: HttpSession.getLastAccessedTime( ): "
            + formatter.format(lastAccessed));
    out.println("</body>");
    out.println("</html>");

  }
}
           
         
    
  








Related examples in the same category

1.Using Sessions in Servlet
2.Session Tracker
3.Servlet: simple session
4.Session logger
5.Servlet: session listener
6.Servlet : session filter
7.Servlet: session attribute listener
8.Servlet: Session bind listener
9.Servlet Session Example
10.Use cookie to save session data
11.Use hidden fields to save session data
12.Use URL rewrite to save session data
13.Session Events: implements HttpSessionBindingListener
14.Session Expiration Filter
15.Map adaptor for HttpSession objects
16.Fake session