1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package filters;
19
20
21 import java.io.IOException;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29
30
31 /**
32 * Example filter that can be attached to either an individual servlet
33 * or to a URL pattern. This filter performs the following functions:
34 * <ul>
35 * <li>Attaches itself as a request attribute, under the attribute name
36 * defined by the value of the <code>attribute</code> initialization
37 * parameter.</li>
38 * <li>Calculates the number of milliseconds required to perform the
39 * servlet processing required by this request, including any
40 * subsequently defined filters, and logs the result to the servlet
41 * context log for this application.
42 * </ul>
43 *
44 * @author Craig McClanahan
45 * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
46 */
47
48 public final class ExampleFilter implements Filter {
49
50
51
52
53
54 /**
55 * The request attribute name under which we store a reference to ourself.
56 */
57 private String attribute = null;
58
59
60 /**
61 * The filter configuration object we are associated with. If this value
62 * is null, this filter instance is not currently configured.
63 */
64 private FilterConfig filterConfig = null;
65
66
67
68
69
70 /**
71 * Take this filter out of service.
72 */
73 public void destroy() {
74
75 this.attribute = null;
76 this.filterConfig = null;
77
78 }
79
80
81 /**
82 * Time the processing that is performed by all subsequent filters in the
83 * current filter stack, including the ultimately invoked servlet.
84 *
85 * @param request The servlet request we are processing
86 * @param result The servlet response we are creating
87 * @param chain The filter chain we are processing
88 *
89 * @exception IOException if an input/output error occurs
90 * @exception ServletException if a servlet error occurs
91 */
92 public void doFilter(ServletRequest request, ServletResponse response,
93 FilterChain chain)
94 throws IOException, ServletException {
95
96
97 if (attribute != null)
98 request.setAttribute(attribute, this);
99
100
101 long startTime = System.currentTimeMillis();
102 chain.doFilter(request, response);
103 long stopTime = System.currentTimeMillis();
104 filterConfig.getServletContext().log
105 (this.toString() + ": " + (stopTime - startTime) +
106 " milliseconds");
107
108 }
109
110
111 /**
112 * Place this filter into service.
113 *
114 * @param filterConfig The filter configuration object
115 */
116 public void init(FilterConfig filterConfig) throws ServletException {
117
118 this.filterConfig = filterConfig;
119 this.attribute = filterConfig.getInitParameter("attribute");
120
121 }
122
123
124 /**
125 * Return a String representation of this object.
126 */
127 public String toString() {
128
129 if (filterConfig == null)
130 return ("InvokerFilter()");
131 StringBuffer sb = new StringBuffer("InvokerFilter(");
132 sb.append(filterConfig);
133 sb.append(")");
134 return (sb.toString());
135
136 }
137
138
139 }
140