Forums Register Login

Hit Counts using Session Tracking

+Pie Number of slices to send: Send
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>" );

}
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 736 times.
Similar Threads
Servlet Invocation
About create a session!
whilst fooling around with SessionId and cookies...
URL rewriting ?
Unusual Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:53:48.