start page | rating of books | rating of authors | reviews | copyrights

Book Home Java Enterprise in a Nutshell Search this book

5.3. Servlet Chaining

So far, we have looked at servlets that take requests directly from the server and return their results directly to the client. Servlets were designed as a generic server extension technology, however, rather than one devoted solely to performing CGI-like functions. A servlet can just as easily take its input from another servlet, and a servlet really doesn't care very much about where its output goes.

Most web servers that implement servlets have also implemented a feature called servlet chaining, where the server routes a request through an administrator-defined chain of servlets. At the end of the sequence, the server sends the output to the client. Alternately, some servers can be configured to route certain MIME types through certain servlets. If a filtering servlet is configured to take all of the output with the MIME type "servlet/filterme," another servlet can produce data with that MIME type, and that data will be passed to the filtering servlet. The filtering servlet, after doing its work, can output HTML for the browser. MIME-based filtering also allows servlets to filter objects that don't come from a servlet in the first place, such as HTML files served by the web server.[4]

[4] It is interesting to note that the Java Web Server is completely servlet-based; it even uses an internal servlet to serve static HTML files. JWS users can easily implement a filtering servlet by chaining it to the end of the file servlet. To use servlet chaining in JWS, you must activate the feature using the administration tool.

Example 5-3 demonstrates a basic servlet, derived from HttpServlet, that examines incoming text for a <DATE> tag and replaces the tag with the current date. This servlet is never called on its own, but instead after another servlet (such as, an HTML generator) has produced the actual content.

Example 5-3. Date Filtering Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class DateFilter extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  
    PrintWriter out = resp.getWriter();

    String contentType = req.getContentType();
    if (contentType == null) 
      return; // No incoming data
    
    // Note that if we were using MIME filtering we would have to set this to 
    // something different to avoid an infinite loop
    resp.setContentType(contentType); 

    BufferedReader br = new BufferedReader(req.getReader());

    String line = null;
    Date d = new Date();
    while ((line = br.readLine()) != null) {
      int index;
      while ((index=line.indexOf("<DATE>")) >= 0) 
        line = line.substring(0, index) + d + line.substring(index + 6);
      out.println(line); 
    }


    br.close();
  }
}

The DateFilter servlet works by reading each line of input, scanning for the text <DATE>, and replacing it with the current date. This example introduces the getReader() method of HttpServletRequest, which returns a PrintReader that points to the original request body. When you call getReader() in an HttpServlet, you can read the original HTTP form variables, if any. When this method is used within a filtering servlet, it provides access to the output of the previous servlet in the chain.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.