Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Count of page hits

 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
Java Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic