import java.io.IOException;
import java.io.PrintWriter;
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 SessionInfo extends HttpServlet {
  static final String key = "SessionInfo.count";

  public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException,IOException
  {
    HttpSession ssn = req.getSession(true);
    res.setContentType("text/html");
    PrintWriter pw = res.getWriter();
    HttpSession session = req.getSession(true);
    int count = 1;
    Integer i = (Integer) session.getAttribute(key);
    if (i != null) {
      count = i.intValue() + 1;
    }
    session.setAttribute(key, new Integer(count));
        pw.println("Your session ID is " + ssn.getId());
    pw.println("<br> and you have hit this page " + count
        + " time(s)during this session");
    pw.println("<form method=GET action=\"" + req.getRequestURI() + "\">");
    pw.println("<input type=submit " + "value=\"Refresh\">");
    pw.println("</form>");
   }
}