• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hit Counts using Session Tracking

 
Chandar S Vellithirumutha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chandar S Vellithirumutha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic