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

public class SessionServlet extends HttpServlet{
	Integer counter1;
	int counter2;
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
		counter2=0;
		//set self environment variable		
	}
	
	public void doGet(HttpServletRequest req,HttpServletResponse resp) 
				 throws IOException{
		HttpSession session=req.getSession(true);
		//set the Response Content Type
		resp.setContentType("text/html");
		//obtain a PrintWriter stream to write to.
		PrintWriter out=resp.getWriter();		
		//print out our Text
		out.println("<html><head><title>Session Servlet</title></head><body>");
	  counter1=(Integer)session.getAttribute("session_counter");
		if(counter1==null){
		  counter1=new Integer(1);	
		}else{
		  counter1=new Integer(counter1.intValue()+1);
		}	
		counter2++;
		
		out.println("You have call the page total <b>"+counter2+"</b> times,");
		out.println("<br>includes from all other browser page.");
		
		/*session.setAttribute("session_counter",counter1);		
		out.println("You have call this page <b>"+counter1+"</b> times.");
		
		//out.println("<br>counter2="+counter2);
		out.println("<br>Click <a href="+resp.encodeURL("SessionServlet")+">href</a>");
		out.println("to access the page again");
		
		out.println("<br>session.isNew():&nbsp;&nbsp;"+session.isNew());		
		//Following,demonstrate some of the more common methods in the HttpSession object surrounding sessions:
		out.println("<h4>RequestData:</h4>");
		out.println("Session ID in Request:&nbsp;&nbsp;"+req.getRequestedSessionId());
		out.println("<br>Session ID in Request from Cookie:&nbsp;&nbsp;"+req.getRequestedSessionId());
		out.println("<br>Session ID in Request from URL:&nbsp;&nbsp;"+req.getRequestedSessionId());
		out.println("<br>Valid Session ID:&nbsp;&nbsp;"+req.isRequestedSessionIdValid());
		
		out.println("<h4>Session Data:</h4>");
		out.println("Session ID:&nbsp;&nbsp;"+session.getId());
		out.println("<br>Create Time:&nbsp;&nbsp;"+session.getCreationTime());
		out.println("<br>LastAccessedTime&nbsp;&nbsp;:"+session.getLastAccessedTime());
		*/
		out.println("</body></html>");
	}
}