本文来自javaeye。。。。
//返回该登陆页面,并予以错误提示信息
response.setContentType("application/xml"); //application/xml代表的是XML形式返回
response.setHeader("Cache-Control", "no-cache"); //设置不缓存
//组织返回数据
String xml="<?xml version="1.0" encoding="gb2312"?>";
xml+="<message>";
xml+="<info>";
xml+="<teacher><name>name01</name><age>20</age></teacher>";
xml+="<teacher><name>name02</name><age>20</age></teacher>";
xml+="<teacher><name>name03</name><age>20</age></teacher>";
xml+="</info>";
xml+="<returnmessage>";
xml+="<returnCode>9999</returnCode>";
xml+="</returnmessage>";
xml+="</message>";
PrintWriter pw=null;
try {
//获取页面写入器
pw=response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
pw.write(xml);
pw.flush();
pw.close();
return null;
JSP里解析获取数据:
<body>
<html:form act
<html:text property="username"></html:text>
<html:checkbox property="check" on
<html:text property="password"></html:text>
<html:submit></html:submit>
<html:reset></html:reset>
<span id="checklogin"/>
</html:form><br><br><br></body>
<script>
var req; //定义全局变量
function usernameCheck()
{
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var url="login.do?method=login&username="+username+"&password="+password;
if(window.XMLHttpRequest)
{
req=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
req=new ActiveXObject("Microsoft.XMLHttp");
}
if(req){
req.open("get",url,true);
req.on
req.send(null);
}
return false;
}
function callback()
{
if(req.readyState==4) //加载完毕
{
if(req.status==200)
{
parseMessage(); //解析数据
}
else
{
alert(req.status);
alert('Not able to litrieve description');
}
}
else
{
//正在加载
document.getElementById("checklogin").innerHTML="正在加载........";
}
}
function parseMessage()
{
this.itemList = new Array();
var xmlDoc=req.responseXML.documentElement; //接收XML格式的数据
var messages=xmlDoc.getElementsByTagName("info");
var mlength=messages.length;
alert(mlength);
if(mlength>0)
{
var teacher=messages[0].getElementsByTagName("teacher");
for( var j=0;j<teacher.length;j++)
{
var name=teacher[j].getElementsByTagName("name")[0].firstChild.nodeValue;
var age=teacher[j].getElementsByTagName("age")[0].firstChild.nodeValue;
alert('No:0'+' name:'+name+'
age:'+age);
this.itemList.push(new Array(name,age));
}
}
var returnmessage=xmlDoc.getElementsByTagName("returnmessage");
var returnCode=returnmessage[0].getElementsByTagName("returnCode")[0].firstChild.nodeValue;
alert(this.itemList.length);
document.getElementById("checklogin").innerHTML=returnCode;
}
</script>