• 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

Keeping track of the requests in a servlet

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked this question quite a few times as how to keep track of the requests to a particular servlet, so I just want to confirm I got it right. Looks like there are few ways to do it; just want to see what everyone thinks is the best way to do so:

a. Storing a static count variable and incrementing it in each of the doXXX methods (). But how can we make sure that different threads are not incrementing at the same time? On what object, should I have my synchronized context upon?
b. Setup a ServletRequestListener and update a context attribute with the count in there.
c. A variation of b - update the context attribute in each of the doXXX methods in a synchronized context of ServletContext.

Thanks!
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would prefer servlet filter as it would be a controller for all servlets
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a servlet filter sounds suitable. But whichever way you go, access to the counter would need to be synchronized.
 
Bharathi Kongara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the responses. Servlet Filter sounds like a better way as the tracking part is most likely not part of the business logic. So I can just update a context attribute with in a synchronized scope of ServeltContext with in the filter instead of doing it in the servlet itself.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

If you want a global counter for a specific servlet, use a static variable in the servlet.

in the servlet doXXX():



@+
Edo...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use a filter as already described, and map the URL to the counter.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servers also have capabilities to track page hits. For example tomcat has the concept of a valve. A valve is basically a server level filter. The advantage of this is that you could have one place to view all pages from the server if you have multiple wars on it.

The open source JAMon can track page hits, as well as response times (min/max/avg), bytes sent (min/max/avg) and more. You can use its servlet filter, valve or other ways to monitor without changing your application. In addition JAMon can track your application exceptions, JDBC performance, logging information and more. You can also keep a buffer that lets you look at your most recent page requests as well as their performance and a stack dump or error if there was one. All this is viewable via the JAMon web application.

Of course there are other products out there too. I guess the main thing is not to consider what is already out there before coding it yourself.

Here is a link that has more about JAMon http monitoring...

http://jamonapi.sourceforge.net/http_monitoring.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic