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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.