package servlet.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.*;

public class VisitListener implements HttpSessionListener {
	private int count;

	public void sessionCreated(HttpSessionEvent arg0) {
		System.out.println("sessionCreated");
		count++;
		HttpSession session = arg0.getSession();
		session.setMaxInactiveInterval(120);
		ServletContext application = session.getServletContext();
		application.setAttribute("visitCount", count);
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
		System.out.println("sessionDestroyed");
		count--;
		if (count <= 0)
			count = 1;
		HttpSession session = arg0.getSession();
		ServletContext application = session.getServletContext();
		application.setAttribute("visitCount", count);
	}
}
