Wednesday, August 8, 2012

Servlet Session in Java

Here is a simple servlet program which display session information.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class sessionInfo extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
    HttpSession mySession = req.getSession(true);
   
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
   
    out.println("<HTML><BODY>");
    out.println("<h3>Session Information</H3>");
    out.println("<BR>New Session    : " + mySession.isNew());
    out.println("<BR>Session ID : " + mySession.getId());
    out.println("<BR>Session Creation Time : " + mySession.getCreationTime());
    out.println("<BR>Session Last Accessed Time : " + mySession.getLastAccessedTime());

    out.println("<BR><H3>Request Information</H3>");
    out.println("<BR>Session ID from Request : " + req.getRequestedSessionId());
    out.println("<BR>Session ID via Cookie : " + req.isRequestedSessionIdFromCookie());
    out.println("<BR>Valid Session ID : " + req.isRequestedSessionIdValid());

    out.println("</BODY></HTML>");
    out.close();
  }
}

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...