[ Team LiB ] Previous Section Next Section

Recipe 18.4 Automatically Refreshing a Servlet

Problem

You want to automatically refresh a servlet-generated page at a specified interval.

Solution

Add a Refresh response header, using the javax.servlet.http.HttpServletResponse object.

Discussion

Suppose that your servlet is monitoring a Red Sox versus Yankees baseball game. You want to be able to allow a user to follow the game almost pitch by pitch, and have your web application constantly update the status of the game. If you add a Refresh response header to your client response, the browser will continually refresh the page according to the specified interval.

Example 18-6 adds a response header that the web container will send to the client in the format Refresh: 60, which means "request this page again in 60 seconds."

Example 18-6. Refreshing a servlet every 60 seconds
package com.jspservletcookbook;           

import javax.servlet.*;
import javax.servlet.http.*;

public class AutoServlet extends HttpServlet {
    
  public void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException,
      java.io.IOException {
    
      //client browser will request the page every 60 seconds
      response.addHeader("Refresh","60");

      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );
      out.println(
          "<html><head><title>Client Refresh</title></head><body>");

      out.println("<h2>Welcome to the Red Sox - Yankees series...</h2>");

      //More HTML or dynamic content
      out.println("</body></html>");

  } //doGet
}

There are some caveats to this approach-if the end user walks away from her desk, her browser will blithely continue to request the page. If your servlet doesn't impose some control over this, you could add a lot of unnecessary load to your application. One example of a solution to this problem is to keep track of how many times the servlet has been refreshed with a session attribute (detailed in Chapter 16). If the number of times exceeds a certain limit, you could stop adding the header to the response. Example 18-7 shows part of a doPost( ) method body for keeping track of a user's refresh count.

Example 18-7. Tracking a user's refresh count
//inside doPost (or doGet) method
HttpSession session = request.getSession( );

Long times = (Long) session.getAttribute("times");

//create session attribute if it doesn't exist
if (times == null)
    session.setAttribute("times",new Long(0));

//local variable 'temp' will hold the session attribute value
long temp = 1;

//increment the attribute value to account for this request
if (times != null)
    temp = times.longValue( ) + 1;

if (temp < 60) //only allow 60 refreshes; about an hour's worth
    response.addHeader("Refresh","60");

//update the session attribute value
session.setAttribute("times",new Long(temp));

This code works equally well inside of a doGet( ) method.


See Also

Recipe 18.5 on automatically refreshing a JSP; Recipe 18.1 and Recipe 18.2 on examining request headers in a servlet and a JSP; Recipe 18.3 on using a filter to wrap the request and forward it along the filter chain; Recipe 18.6 on using a listener to track requests.

    [ Team LiB ] Previous Section Next Section