Listing 1

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Stack;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import weblogic.servlet.FutureResponseServlet;
import weblogic.servlet.FutureServletResponse;

// An AsynchronousServlet that handles HTTP requests from a "separate" thread and
// not the execute thread used to invoke this servlet.
public class AsynchronousServerResponseServlet extends FutureResponseServlet {
	private final Notifier notifier;

	public AsynchronousServerResponseServlet() {
		this.notifier = new Notifier();
		this.notifier.start();
	}

	public void service(HttpServletRequest request, FutureServletResponse response)
		throws IOException, ServletException {

		// push this client's request to a buffer and return immediately.
		// asynchronous processing occurs in the run method of the Notifier Thread
		notifier.poll(request, response);
	}

	class Notifier extends Thread {

		private static Stack clients = new Stack();

		void poll (HttpServletRequest request, FutureServletResponse response) {
			clients.push(new Client(request, response));
		}

		public void run() {
			while (!clients.empty()) {
				Client client = null;
				try
				{
					client = (Client) clients.pop();
					PrintWriter pw = client.response.getWriter();
					for(int j = 0; j < 10; j++) {
						pw.println("Time is:" + new Date() + "<BR>");
						pw.flush();

					}
					pw.close();
				} catch(Throwable t) {
					t.printStackTrace();
				} finally {
					try {
					client.response.send();

					} catch(IOException ioe) {
					ioe.printStackTrace();
					}
				}

			}
		}
	}

	// inner class that holds on to the clients http request and response
	class Client {
		private HttpServletRequest request;
		private FutureServletResponse response;

		private Client(HttpServletRequest request, FutureServletResponse response) {
			this.request = request;
			this.response = response;
		}
	}
}