4 (Example: A Logging Filter) prints an entry in the
log file every time the associated servlet or JSP page is accessed. Suppose you want to
modify it so that it only notes accesses that occur at unusual times. Because ???unusual???
is situation dependent, the servlet should provide default values for the abnormal
time ranges and let deployers override these values by supplying initialization parameters.
To implement this functionality, we implement the following capabilities.
1. A class that implements the Filter interface. This class is called
LateAccessFilter and is shown in Listing 5.10. The init method
of this class reads the startTime and endTime initialization parameters.
It attempts to parse these values as type int, using default values
if the parameters are null or not formatted as integers. It then
stores the start and end times, the FilterConfig, the Servlet-
Context, and the filter name in fields (instance variables) of the filter.
Finally, LateAccessFilter provides an empty body for the
destroy method.
2. Filtering behavior in the doFilter method. This method looks
up the current time, sees if it is within the range given by the start and
end times, and prints a log entry if so.
3. A call to the doFilter method of the FilterChain. After printing
the report, the filter calls the doFilter method of the Filter-
Chain to invoke the next filter in the chain (or the servlet or JSP page
if there are no more filters).
Pages:
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274