2.8. An Example Using Servlet Initialization and Page Modification Dates

Listing 2.11 shows a servlet that uses init to do two things. First, it builds an array of 10 integers. Since these numbers are based upon complex calculations, I don't want to repeat the computation for each request. So I have doGet look up the values that init computed instead of generating them each time. The results of this technique are shown in Figure 2-8.

Figure 2-8. Output of LotteryNumbers servlet.

However, since all users get the same result, init also stores a page modification date that is used by the getLastModified method. This method should return a modification ...

Get Core Servlets and JavaServer Pages™ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.