• Post Reply Bookmark Topic Watch Topic
  • New Topic

Site Hit Counter Question  RSS feed

 
Mike London
Ranch Hand
Posts: 1340
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to program a site hit counter.
The way I'm thinking about doing it is to just use a text file and store the current cumulative visitor number in it. The text file would only have a single integer value in it. (The JSP file would show it in a nice font, color, etc.)
Method: using File classes, I'd open the text file, read the integer from it, increment it, and write back the new cumulative number.
I'd also make sure I could actually open the file in the open code above in case another web user had it open at the same time.
Does this seem like a good way to implement a site hit counter? This is a fairly low-volume web site.
I'd appreciate any replies or advice.
Thanks.
-- Mike
 
Gary McGath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic idea is right, but there are a few ways you can improve it.
First, make sure the method which updates the file is synchronized, so different invocations of your page won't interfere with each other.
Second, keep the counter in memory and overwrite the file each time. This is more efficient than reading the file and writing it back.
You might also consider some code for not writing the file more than once every 5 seconds (using a static variable indicating the time of the last write), but this may not be important for a low-volume site.
 
Mike London
Ranch Hand
Posts: 1340
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Gary,
Thanks for all the good ideas!!!
I appreciate your reply.
Thanks again.
-- Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!