1----------------------------------------------------
web.xml
dynMusic
sentrandomvalues
ajaxtest.SentRandomValues
sentrandomvalues
/sentrandomvalues
2-----------------------------------------------
index.jsp
--
index
width="100%" height="100%">
3,----------------------------------------------------------------
musicRec.svg
/p>
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
function timeEvent(){
showCustomer();
setTimeout("timeEvent()",1000);
}
function showCustomer()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="sentrandomvalues";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
random1=xmlDoc.getElementsByTagName("para")[0].childNodes[0].nodeValue;
random2=xmlDoc.getElementsByTagName("para")[2].childNodes[0].nodeValue;
random3=xmlDoc.getElementsByTagName("para")[1].childNodes[0].nodeValue;
random4=xmlDoc.getElementsByTagName("para")[3].childNodes[0].nodeValue;
create_a(random1,random2,random3,random4);
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function create_a(random1,random2,random3,random4)
{
var rect1=document.getElementById("rect1");
var rect2=document.getElementById("rect2");
var rect3=document.getElementById("rect3");
var rect4=document.getElementById("rect4");
var text1=document.getElementById("text1");
var text2=document.getElementById("text2");
var text3=document.getElementById("text3");
var text4=document.getElementById("text4");
rect1.setAttribute("height",250-random1);
rect1.setAttribute("y",2+eval(random1));
rect2.setAttribute("height",250-random2);
rect2.setAttribute("y",2+eval(random2));//
如果写成2+random2时,会出现字符串相加
rect3.setAttribute("height",250-random3);
rect3.setAttribute("y",2+eval(random3));
rect4.setAttribute("height",250-random4);
rect4.setAttribute("y",2+eval(random4));
}
fill="none" stroke="blue" stroke-width="2" />
//
//
//
//
4,----------------------------------------------------------
SerntRandomValues.java
package ajaxtest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class SentRandomValues extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/xml");
response.setHeader("Cache-Control","no-cache");
StringBuffer sb=new StringBuffer("");
sb.append("");
sb.append(((Math.random()*250)));
sb.append("");
sb.append("");
sb.append(((Math.random()*250)));
sb.append("");
sb.append("");
sb.append(((Math.random()*250)));
sb.append("");
sb.append("");
sb.append(((Math.random()*250)));
sb.append("");
sb.append("");
PrintWriter out = response.getWriter();
System.out.println(sb.toString());
out.print(sb.toString());
out.flush();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources public void destroy() { }}