Oracle GlassFish Server 3.0.1 Application Development Guide

ProcedureTo Create a Web Component to Support Comet

  1. Create an empty servlet class, like the following:

    import javax.servlet.*;
    public class HiddenCometServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    private String contextPath = null;
    	public void init(ServletConfig config) throws ServletException {}
    	protected void doGet(HttpServletRequest req, 
    	HttpServletResponse res) 
    	throws ServletException, IOException {}
    	protected void doPost(HttpServletRequest req, 
    	HttpServletResponse res)
    	throws ServletException, IOException {);
  2. Import the following Comet packages into the servlet class:

    import com.sun.grizzly.comet.CometContext;
    import com.sun.grizzly.comet.CometEngine;
    import com.sun.grizzly.comet.CometEvent;
    import com.sun.grizzly.comet.CometHandler;
  3. Import these additional classes that you need for incrementing a counter and writing output to the clients:

    import java.util.concurrent.atomic.AtomicInteger;
  4. Add a private variable for the counter:

    private final AtomicInteger counter = new AtomicInteger();