aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Count the webpage hits? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Count the webpage hits?" Watch "Count the webpage hits?" New topic
Author

Count the webpage hits?

Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117
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
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
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 ]
 
 
subject: Count the webpage hits?
 
Threads others viewed
Hit Counter
Creating a hit counter in JSP.
Chapter 6(Session Management) notes (HFSJ) for revision
long post IBM.158
how to count number of visitors
IntelliJ Java IDE