FileDocCategorySizeDatePackage
AdMaker.javaAPI DocExample1251Wed Apr 05 11:25:42 BST 2000None

AdMaker.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.*;

public class AdMaker extends HttpServlet {
  
  static String[] adText = { "Al's Web Services", 
                             "Bob's House of HDs",
                             "Main St. Computers" };
  int currentAd = 0; 

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    
    String adContent;
    PrintWriter out = resp.getWriter();
    synchronized(this) {
      adContent = adText[currentAd];
      currentAd++;
      if (currentAd >= adText.length)
        currentAd = 0;
    } 
    String title = req.getParameter("pagetitle");
    if(title != null)
      out.println(title + " is brought to you by");
    else
      out.println("This page is brought to you by");
    out.println(adContent);
  }
}