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

public class RewriteServlet extends HttpServlet{

static final String COUNTER_KEY = "RewriteCounter.count";
public void doGet(HttpServletRequest req,HttpServletResponse resp)
				throws ServletException, java.io.IOException{
		resp.setContentType("text/html");
		java.io.PrintWriter out = resp.getWriter();
		HttpSession session = req.getSession(true);
		//Is there a count yet?
		int count =1;
		Integer i = (Integer) session.getValue(COUNTER_KEY);
		//If a previous count exists, set it
		if(i!= null){
			count = i.intValue() + 1;
		}
		//Put the count back into the session
		session.putValue(COUNTER_KEY, new Integer(count));

		//Print a standard header
		out.println("<html><head><title>Session Counter with URL rewriting</title>");
		out.println("</head><body>");
		out.println("<br>session ID is <b>"+session.getId()+"</b>");
		out.println("<br>you hit this page<b>"+count+"time(s)");
		String url="RewriteServlet";
		out.println("<br>resp.encodeUrl(url)=<b>"+resp.encodeUrl(url)+"</b>");
		out.println("<form method=POST action="+resp.encodeUrl(url)+">");
		out.println("<input type=submit value=\"Hit page again\">");
		out.println("</form></body></html>");
	}
	public void doPost(HttpServletRequest req,HttpServletResponse resp)
			throws ServletException, java.io.IOException{
		doGet(req,resp);
	}
}
