Sun GlassFish Enterprise Server v3 Application Development Guide

ProcedureTo Create a HTML Page That Updates and Displays the Content

  1. Create an HTML page called count.html and add the following content to it:

    <html>
    	<head>
    	</head>
    		<body>
    			<center>
    				<h3>Comet Example: Counter with Hidden Frame</h3>
    				<p>
    				<b id="count">&nbsp;</b>
    				<p>
    			</center>
    		</body>
    </html>

    This page displays the current count.

  2. Add JavaScript code that updates the count in the page. Add the following lines in between the head tags of count.html:

    <script type='text/javascript'>
    	function updateCount(c) {
    		document.getElementById('count').innerHTML = c;
    		parent.hidden.location.href = "hidden_comet";
    	};
    </script>

    The JavaScript takes the updated count it receives from the servlet and updates the count element in the page. The last line in the updateCount function invokes the servlet's doGet method again to reestablish the connection.

    • For HTTP-Streaming:

      Add the same code as for long-polling, except for the following line:

      parent.hidden.location.href = “hidden_comet”

      This line invokes the doGet method of CometServlet again, which would reestablish the connection. In the case of HTTP-Streaming, you want the connection to remain open. Therefore, you don't include this line of code.