Simple Example of Ajax Implementation of Automatic refresh (crazy Ajax)
Second. jsp
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <br/> <% @ page import =" Java. util. random "%> <br/> <% <br/> random Rand = new random (system. currenttimemillis (); <br/> out. println (Rand. nextint (10) + "$" + Rand. nextint (10) + "$" + Rand. nextint (10); <br/>%> <br/>
1. html
<Body> <br/> the mysql virtual stock price is: <div id = "mysql" style = "color: red; font-weight: bold;" mce_style = "color: red; font-weight: bold; "> </div> <br/> the virtual stock price of tomcat is: <div id =" tomcat "style =" color: red; font-weight: bold; "mce_style =" color: red; font-weight: bold; "> </div> <br/> jetty's virtual stock price is: <div id = "jetty" style = "color: red; font-weight: bold;" mce_style = "color: red; font-weight: bold; "> </div> <br/> <mce: script type =" text/plain cr Ipt "> <! -- <Br/> var xmlrequest; <br/> function createXMLHttpRequest () <br/>{< br/> if (window. XMLHttpRequest) <br/>{< br/> xmlrequest = new XMLHttpRequest (); <br/>}< br/> else if (window. activeXObject) <br/>{< br/> try <br/>{< br/> xmlrequest = new ActiveXObject ("Msxm12.XMLHTTP "); <br/>}< br/> catch (e) <br/>{< br/> try <br/>{< br/> xmlrequest = new ActiveXObject ("Microsoft. XMLHTTP "); <br/>}< br/> catch (e) {}< br/>}< br/> function getPrice () <br/>{< br/> createXMLHttpRequest (); <br/> var uri = "second. jsp "; <br/> xmlrequest. open ("POST", uri, true); <br/> xmlrequest. onreadystatechange = processResponse; <br/> xmlrequest. send (null); <br/>}< br/> function processResponse () <br/>{< br/> if (xmlrequest. readyState = 4) <br/>{< br/> if (xmlrequest. status = 200) <br/>{< br/> var prices = xmlrequest. responseText. split ("$"); <br/> document. getElementById ("mysql "). innerHTML = prices [0]; <br/> document. getElementById ("tomcat "). innerHTML = prices [1]; <br/> document. getElementById ("jetty "). innerHTML = prices [2]; <br/> setTimeout ("getPrice ()", 1000 ); <br/>}< br/> document. body. onload = getPrice; <br/> // --> </mce: script> <br/> </body>