javaweb-servicesrestservletsservlet-listeners

Request counter for individual all the web service api


I have created web services for my project, using rest and some of using only servlet.

I want to create request counter for individual all the web service api to analyse individual request ratio per hour,per day and many more.

I don't want to put counter for every api. Can I achieve it by using Servlet Listner or Servlet Filter? yes then how??? Small demo would be great if possible.

Note : I know that I can analyse most of the details from access log.


Solution

  • Simplest solution would be to create and register a Filter.

    In the web.xml you can use wildcards (*) to map it to any urls or just the urls of the Servlets you want to count, so one Filter could do the counting for all your Servlets including the custom ones you create or any shipped Servlets that come from 3rd party libraries (because they are also created and mapped in your web.xml to the URL patterns of your choice).

    In Filters you can also provide additional filters whether you want to count a call based in the url, parameters etc.

    Serving all requests that match the URL patterns of any registered Filters starts and ends in the code of the Filters, so in a Filter you also have the possibility to do the counting before or after serving the requests. You also have the possibility to count based on the response status code etc.

    So in a Filter you can do an advanced counting like counting all requests, their average execution time, error rate, average received and sent bytes etc. also broken down to Servlets.