FileDocCategorySizeDatePackage
DateFilter.javaAPI DocExample1341Wed Apr 05 11:25:42 BST 2000None

DateFilter.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

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();
  }
}