• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Count of page hits

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 377
Android Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic