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.
Yes, a servlet filter sounds suitable. But whichever way you go, access to the counter would need to be synchronized.
Joined: Sep 15, 2005
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.
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...