Chapter 6 ?– The Application Events Framework 314
6.11 Identifying Servlet Request
Initialization and Destruction
We use ServletContextListener (see Section 6.1) and SessionListener
(see Section 6.7) to monitor creation and destruction of the ServletContext and
HttpSession, respectively. Version 2.4 of the servlet specification introduced a
new listener, ServletRequestListener, which allows us to detect creation and
destruction of each ServletRequest.
The steps that involve using ServletRequestListener are very similar to the
steps for using ServletContextListener and SessionListener.
1. Implement the ServletRequestListener interface. This interface
is located in the javax.servlet package.
2. Implement requestInitialized and requestDestroyed.
The first of these (requestInitialized) is triggered right before a
new ServletRequest is processed by a Web component (e.g., a
servlet or a filter). The second method (requestDestroyed) is triggered
when a ServletRequest object is about to go out of scope of
the processing Web component and become eligible for garbage
collection.
3. Obtain a reference to the request and possibly to the servlet
context and session. Each of the two ServletRequestListener
methods takes a ServletRequestEvent as an argument. The
ServletRequestEvent class has a getServletRequest method
that provides access to the request object. Note that the return type of
the getServletRequest method is ServletRequest, not
HttpServletRequest.
Pages:
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366