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

Count of page hits

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 109
I need to count Page hit for every page. Every page should show the number of times the said page has been accessed. I need to show message like "This is 20th visit to this page"

I have searched the code ranch forum and googled and have found below solutions.

There are multiple approaches:
a: Use DB to store and return every page visit
b. Use a custom tag to implement the page hit counter. This approach has below issues
(i)The issue is that the counter would be lost when the app will be restarted. Thus would need to persist the counter for every page and re-initialize counter again for every page. Any idea on how can I get a handle to all pages' hit counter in a ServletContextListener(is there a API support for this)
(ii) This may not work correctly in a clustered application and thus there will be multiple instances of same page and thus different counts. I am looking for any suggestions to resolve this.

I feel solution a) might be a lot of over engineering for a little stuff. But I would like to hear what other people have to say and how they have handled it in their applications.
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Pankaj,

May be you can use a file which just contains the count and everytime you can increment the count in the doPost/doGet method exits, if you really do not want to use the DB
But again, as the unwritten rule for any software problem is there are n number of ways to solve it.


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

There may be other things you want to take into consideration, do you WANT to update EVERY time the page updates?

For example if I go to your page and hold down F5 do you want every single page refresh to count as a page access?

You may only want to let a user update the page count for a certain page once per session.

This is similar to how google analytics works (Or maybe you can even use that to get the counts... I'm not sure).

[edit]Personally I would use a filter to update a database and to set the count for that page into the request to use in your JSP, that is a personal preference though.[/edit]

Sean


I love this place!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Count of page hits