Context Attributes Servlet : Context « Servlets « Java






Context Attributes Servlet


import java.io.PrintWriter;
import java.io.IOException;
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 SessionTracker2 extends HttpServlet 
{
  public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession(true);

    Integer totalCount = (Integer) getServletContext().getAttribute("total");

    if (totalCount == null)
    {
      totalCount = new Integer(1);
    }
    else
    {
      totalCount = new Integer(totalCount.intValue() + 1);
    }

    Integer count = (Integer) session.getAttribute("count");

    if (count == null) {
      count = new Integer(1);
    } else {
      count = new Integer(count.intValue() + 1);
    }

    session.setAttribute("count", count);
    getServletContext().setAttribute("total", totalCount);

    out.println("<html><head><title>SessionSnoop</title></head>");
    out.println("<body><h1>Session Details</h1>");
    out.println("You've visited this page " + count + ((count.intValue() == 1) ? " time." : " times.") + "<br/>");
    out.println("Total number of visits: " + totalCount + "<br/>");
    out.println("<h3>Details of this session:</h3>");
    out.println("Session id: " + session.getId() + "<br/>");
    out.println("New session: " + session.isNew() + "<br/>");
    out.println("Timeout: " + session.getMaxInactiveInterval() + "<br/>");
    out.println("Creation time: " + new Date(session.getCreationTime()) + "<br/>");
    out.println("Last access time: " + new Date(session.getLastAccessedTime()) + "<br/>");
    out.println("</body></html>");
  }
}

           
       








ContextAttributesServlet.zip( 90 k)

Related examples in the same category

1.Servlets Context Sample
2.Servlets ServletContextListener Demo
3.Context log
4.Context logger
5.Context binder
6.Context accessor
7.Set the context parameters in web.xml
8.Log in ServletContext
9.Using Contexts Servlet
10.Context Parameters Servlet
11.Get settings from ServletContext