• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Count the webpage hits?

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers,
I have built a JSP where I keep count of the client visits to a calling static web page.
I know this sounds like a simple task but i need to store and retrieve the count value after stopping and restarting server.
Tell me if Im on the right track:
I have created a bean: CountBean.java to store the number of hits for a web page. When the server page is loaded - the beans constructor reads an integer from a file ( the last count ) and stores it. Each subsequent time the server page is loaded - the beans increment method is called to update the visitor count variable. In the Bean - I have overridden the finalize method to write the final count back to the file.
Question: When the server is stopped - is there a garbage collection process? - and if so, should it call the finalize method on the Bean object.
At the moment the value of count is reset each time the server is restarted.
Should I be using a cookie instead. And if so, will a cookie persist beyond the server reboot??
Regards
Stephen Batsas
SCPJ2
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephen Batsas:

Question: When the server is stopped - is there a garbage collection process? - and if so, should it call the finalize method on the Bean object.
At the moment the value of count is reset each time the server is restarted.


Stephen,
one of my most important rules as a java programmer is to NEVER_TRUST_GARBAGE_COLLECTION (we learn that in scjp-exam by the way) .
Remember that servlets have a life-cycle. Why not put make_persistent code by simply overwriding destroy() method of the class GenericServlet?
You could try put it into a jsp - declaration (perhaps there is a better way for jsps I dont know):
<%! public void destroy() { do_serialization_here } %>
By the way: You dont need a bean. You could put the hit counter in a declaration, too.
<%! long counter = 0; %>
And then increment it through a simple scriptlet.
<% counter++; %>
As this is not thread-safe, one might think about making access to counter synchronized.
Axel
[ March 09, 2002: Message edited by: Axel Janssen ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic