Because we need to store our request records
where any user can retrieve the statistics at a later time, we need to
obtain a reference to the ServletContext. We accomplish this by
passing the ServletRequestEvent object to the helper method
called getRequestRecords and calling getServletContext
on it.
4. Use the objects. The helper getRequestRecords method is called
by another helper method called recordRecord from the request-
Initialized method. The getRequestRecords method retrieves
an ArrayList as an attribute with the key of requestRecords from
the ServletContext (or creates a new one if such an attribute
doesn??™t already exist). The recordRecord method adds the current
system time in milliseconds as an item to the list. This approach allows
us to record the total number of requests (e.g., total number of items in
the list) together with the time the first request and the last request are
made (e.g., first item??™s value in the list and last item??™s value in the list).
Using this information we are able to calculate request frequency as a
number of requests per second.
We use the ProcessRequestStats servlet together with the
req-stats.jsp page to display the statistics we have gathered over time.
Listing 6.31 and Listing 6.32 show the code for ProcessRequest-
Stats.java and req-stats.jsp, respectively. The doGet method of the
ProcessRequestStats servlet retrieves the ServletContext
attribute with the key of requestRecords, which is the ArrayList
that contains the request records gathered over some time.
Pages:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370