I am trying to find the hit counts using HttpSession object and i don't know why this code is not working.... Can any one help me in this.... Thanks a bunch Chandar x48588 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
HttpSession session = req.getSession(true);
if (session.isNew()) { synchronized(this) { hitcounts++; } } out.println("<html><title>Counts</title><body>");
out.println("<b>No. of visitors to the site = " +hitcounts + "</b>" );
}
William Brogden
,
Author and all-around good cowpoke
What do you mean by not working? Is hitcounts always 1 or what? Which servlet engine are you using? Have you tried debugging techniques like System.out.println calls sprinkled around?
William said: "Have you tried debugging techniques like System.out.println calls sprinkled around?" I know we may be "showing our age" but that is my favorite debuggin' tool! Ha!
I am using jswdk1.0.1. It is working when I connect to internet but not when i am offline even though the server is on. May i know for that please. Thanks for your help bill
William Brogden
,
Author and all-around good cowpoke
I am still not clear on your setup and what you mean by not working. Where does the servlet code live when you "connect to internet" and where does it live when you are "offline". What do you mean by It is working when I connect to internet but not when i am offline even though the server is on.
From the documentation of ServletResponse (super class of HttpServletResponse) If you will be using a writer, and want to call the setContentType method, you must do so before calling the getWriter method. This is probably the source of the error that you are recieving. Additionally I'm assuming that hitcounts is defined as a static class variable. Try making the change and ensuring the variable is defined properly and it should work.
Post by:autobot
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards