KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > web > TimerFilter


1
2 package test.web;
3
4 import javax.servlet.*;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 /**
9  * Timer filter that logs the request processing time in milliseconds to the console.
10  *
11  * <p>A filter is an object that performs filtering tasks on either the request to a resource
12  * (a servlet or static content), or on the response from a resource, or both.</p>
13  *
14  * <p>For more information on filters, check the <a HREF="http://java.sun.com/products/servlet/2.3/javadoc/">javadoc</a>
15  * or read the <a HREF="http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters_p.html">Filter code with Servlet 2.3 model</a>
16  * article at <a HREF="http://www.javaworld.com">JavaWorld</a>.</p>
17  *
18  * @web.filter
19  * display-name="Timer Filter"
20  * name="TimerFilter"
21  *
22  * @web.filter-init-param
23  * name="param1"
24  * value="value1"
25  *
26  * @web.filter-init-param
27  * name="param2"
28  * value="value2"
29  *
30  * @web.filter-mapping
31  * url-pattern="*.xml"
32  *
33  * @author <a HREF="mailto:youremail@yourdomain.com">youremail@yourdomain.com</a>
34  */

35 public class TimerFilter implements Filter {
36     /**
37      * A filter configuration object used by a servlet container used to pass information to
38      * a filter during initialization. This is set in the <code>init(FilterConfig config)</code> method.
39      */

40     private FilterConfig config = null;
41
42     /** Called by the web container to indicate to a filter that it is being placed into service. */
43     public void init(FilterConfig config) throws ServletException {
44         this.config = config;
45     }
46
47     /** Called by the web container to indicate to a filter that it is being taken out of service. */
48     public void destroy() {
49         config = null;
50     }
51
52     /**
53      * Logs the time it took in milliseconds to process the request.
54      *
55      * <p>The <code>doFilter</code> method of the Filter is called by the container each time a
56      * request/response pair is passed through the chain due to a client request for a
57      * resource at the end of the chain.
58      */

59     public void doFilter(ServletRequest JavaDoc request, ServletResponse response,
60                          FilterChain chain) throws IOException JavaDoc, ServletException {
61         long before = System.currentTimeMillis();
62
63         // pass the request and response to the filter chain
64
chain.doFilter(request, response);
65
66         long after = System.currentTimeMillis();
67
68         String JavaDoc name = "";
69         if (request instanceof HttpServletRequest JavaDoc)
70             name = ((HttpServletRequest JavaDoc) request).getRequestURI();
71
72         // log the difference in time, check the console for output
73
config.getServletContext().log(name + ": " + (after - before) + "ms");
74     }
75 }
Popular Tags