Recipe 18.6 Counting the Number of Web Application Requests

Recipe 18.6 Counting the Number of Web Application Requests


You want to count the number of requests handled by a web application.


Use a javax.servlet.ServletRequestListener to be notified whenever an HTTP request is initialized .


A request listener is a good candidate for tracking requests, because the web container notifies the listener of new requests by calling its requestInitialized( ) method. Example 18-8 keeps track of the request count with a static class variable named reqCount . The program increments this variable in a synchronized block within the requestInitialized( ) method.

The ServletContext is used to log a message about the request so that you can observe the listener behavior. However, a busy production application that logs information about every request typically represents an inefficient use of web container resources. This type of logging activity should be reserved for development applications.

Example 18-8. A request listener class for counting application requests
 package com.jspservletcookbook; import javax.servlet.*; import javax.servlet.http.*;  public class ReqListener implements ServletRequestListener {  private static long reqCount;  public void requestInitialized(ServletRequestEvent sre){       //used for logging purposes       ServletContext context = sre.getServletContext( );       //Used to get information about a new request       ServletRequest request = sre.getServletRequest( );           //The static class variable reqCount is incremented in this block;       //the incrementing of the variable is synchronized so that one       // thread is not reading the variable while another increments it       synchronized (context){           context.log(             "Request for "+               (request instanceof HttpServletRequest ?               ((HttpServletRequest) request).getRequestURI( ) :               "Unknown")+ "; Count="+ ++reqCount);       }//synchronized }  public void requestDestroyed(ServletRequestEvent sre){     //Called when the servlet request is going oout of scope.   }//requestDestroyed } 

You can access the new ServletRequest in the two ServletRequestListener methods by calling ServletRequestEvent.getServletRequest( ) . You must cast the ServletRequest return value to an HttpServletRequest to call the latter class's methods. Example 18-8 accesses the new HttpServletRequests in order to call those object's getRequestURI( ) method, which provides part of the information the code includes in a logging message.

You must register the ServletRequestListener in web.xml :

 <listener>     <listener-class>com.jspservletcookbook.ReqListener</listener-class> </listener> 

The web container then creates an instance of the listener when it starts up. Here is an example of a server-log entry when a request is made to the application within which the request listener is registered:

 2003-05-30 07:22:21 Request for /home/servlet/com.jspservletcookbook.SessionDisplay;  Count=2 

For Tomcat, this line would be displayed in the log file found in <Tomcat-installation-directory>/logs .

See Also

Recipe 18.1 and Recipe 18.2 on examining request headers in a servlet and a JSP; Recipe 18.3 on using a filter to wrap the request and forward it along the filter chain; Chapter 7 on handling request parameters and JavaBean properties with servlets, JSPs, and filters.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: