This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Counting no. of hits to servlet ?

 
Pravin Reddy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic