my dog learned polymorphism
The moose likes Servlets and the fly likes Keeping track of the requests in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Keeping track of the requests in a servlet" Watch "Keeping track of the requests in a servlet" New topic

Keeping track of the requests in a servlet

Bharathi Kongara
Ranch Hand

Joined: Sep 15, 2005
Posts: 56
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.

Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
I would prefer servlet filter as it would be a controller for all servlets
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Yes, a servlet filter sounds suitable. But whichever way you go, access to the counter would need to be synchronized.
Bharathi Kongara
Ranch Hand

Joined: Sep 15, 2005
Posts: 56
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

Joined: Apr 09, 2009
Posts: 11

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

in the servlet doXXX():


SCJP 5.0 87%
David Newton

Joined: Sep 29, 2008
Posts: 12617

Or use a filter as already described, and map the URL to the counter.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
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... - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
I agree. Here's the link:
subject: Keeping track of the requests in a servlet
It's not a secret anymore!