Add an onEvent method to the CometHandler implementation class to define what happens when an event occurs:
The last line resumes the Comet request and removes it from the list of active CometHandler objects. By this line, you can tell that this application uses the long-polling technique. If you were to delete this line, the application would use the HTTP-Streaming technique.
Add the same code as for long-polling, except do not include the following line:
You don't include this line because you do not want to resume the request. Instead, you want the connection to remain open.
Increment the counter and forward the response by adding the following lines to the doPost method:
counter.incrementAndGet(); CometEngine engine = CometEngine.getEngine(); CometContext<?> context = engine.getCometContext(contextPath); context.notify(null); req.getRequestDispatcher("count.html").forward(req, res);
When a user clicks the button, the doPost method is called. The doPost method increments the counter. It then obtains the current CometContext object and calls its notify method. By calling context.notify, the doPost method triggers the onEvent method you created in the previous step. After onEvent executes, doPost forwards the response to the clients.