This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keeping track of the requests in a servlet

 
Bharathi Kongara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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!
 
Poobhathy Kannan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer servlet filter as it would be a controller for all servlets
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Eddy Pelaic
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use a filter as already described, and map the URL to the counter.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic