An Example Using Servlet Initialization and Page Modification Dates

By: aathishankaran Viewed: 153224 times  Printer Friendly Format    


The program 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. However, since all users get the same result, init also stores a page modification date that is used by the getLastModified method.

 LotteryNumbers.java


import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
public class LotteryNumbers extends HttpServlet {  
private long modTime;  
private int[] numbers = new int[10];

public void init() throws ServletException {

// Round to nearest second (ie 1000 milliseconds)  
modTime = System.currentTimeMillis()/1000*1000;  
for(int i=0; i<numbers.length; i++) {  
numbers[i] = randomNum();  
}

}  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
throws ServletException, IOException {  

response.setContentType("text/html");  
PrintWriter out = response.getWriter();  
String title = "Your Lottery Numbers";  
out.println(ServletUtilities.headWithTitle(title) +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +  
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +  
"<B>Based upon extensive research of " +  
"astro-illogical trends, psychic farces, " +  
"and detailed statistical claptrap, " +  
"we have chosen the " + numbers.length +  
" best lottery numbers for you.</B>" +  
"<OL>");

for(int i=0; i<numbers.length; i++) {  
out.println(" <LI>" + numbers[i]);  
}

out.println("</OL>" +

"</BODY></HTML>");

This method should return a modification time expressed in milliseconds since 1970, as is standard with Java dates. The time is automatically converted to a date in GMT appropriate for the Last-Modified header. More importantly, if the server receives a conditional GET request (one specifying that the client only wants pages marked If-Modified-Since a particular date), the system compares the specified date to that returned by getLastModified, only returning the page if it has been changed after the specified date.

public long getLastModified(HttpServletRequest request) {

return(modTime);

}

// A random int from 0 to 99.

private int randomNum() {

return((int)(Math.random() * 100));

}

}

Browsers frequently make these conditional requests for pages stored in their caches, so supporting conditional requests helps your users as well as reducing server load. Since the Last-Modified and If-Modified-Since headers use only whole seconds, the get-LastModified method should round times down to the nearest second. It shows the result of requests for the same servlet with two slightly different If-Modified-Since dates. To set the request headers and see the response headers, I used WebClient, a Java application shown in previous article (WebClient: Talking to Web Servers Interactively) that lets you interactively set up HTTP requests, submit them, and see the results.



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial