aspose file tools*
The moose likes Servlets and the fly likes Counting no. of hits to servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Counting no. of hits to servlet ?" Watch "Counting no. of hits to servlet ?" New topic
Author

Counting no. of hits to servlet ?

Pravin Reddy
Greenhorn

Joined: Dec 01, 2004
Posts: 8
Hi friends,

I need to find the count of no. of hits to my servlet page whenever a user enters the page.

u1 enters it should show some no. 2000
u2 enters it should show 2001 even if the u1 didn't log out
u3..... 2002
u4......2003
..
..
..

Even if the application crashes .... server is down, the variable count where is stored permanantly?

Please guide me

bye
pravin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You could roll your own or you could use whatever access-logging features your app-server provides.

The access logs tend to have a lot more information than you really need but, thankfully, there are plenty of products out there, such as WebTrends, that can parse this file and return readable results.

Tomcat, for instance, shipps with an access log valve which can be turned on by uncommenting it's entry in server.xml.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike Cronin
Greenhorn

Joined: Apr 17, 2002
Posts: 25
Add a static hit counter to your servlet. Create a couple of methods, one that increments the static counter then stores that value to a simple text file, and second method that reads the simple text file (for this example let's call the text file hits.txt).

On your servlet init method, determine whether or not the hits.txt file exists, if it does, initialize your static counter to the last value stored in your hits.txt file.

Once the servlet has been initialized, you simply increment your static counter and update your hits.txt file for each thread that enters your servlet (you should enclose this functionality within a synchronized method).

Throughout your servlet, keep thread synchronization in mind when dealing with your static counter.

Hope this helps.

Mike Cronin
Data On Call
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting no. of hits to servlet ?