在这个jsp文件中上面的xml,通过responseText和innerHTML被解析成为一个表格。
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>一个简单的例子</title>
<script type="text/javascript" language="javascript">
var xmlHttp = false;
function makeRequest(url) {
xmlHttp = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType(‘text/xml‘);
}
} else if (window.ActiveXObject) { // IE
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlHttp) {
alert(‘Giving up :( Cannot create an XMLHTTP instance‘);
return false;
}
xmlHttp.onreadystatechange = alertContents;
xmlHttp.open(‘GET‘, url, true);
xmlHttp.send(null);
}
function alertContents() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
document.getElementById("results").innerHTML+=xmlHttp.responseText;
} else {
alert(‘There was a problem with the request.‘);
}
}
}
</script>
</head>
<body>
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest(‘innerHTML.xml‘)" id="results">
Make a request
</span>
</body>
</html>
我们将在parseXML.jsp中显示parseXML.xml中所有state标记的值。
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<script language="javascript">
var xmlHttp=false;
function createXMLHttpRequest(){
if (window.XMLHttpRequest) { // Mozilla, Safari,...
xmlHttp= new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType(‘text/xml‘);
}
} else if (window.ActiveXObject) { // IE
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlHttp) {
alert(‘Giving up :( Cannot create an XMLHTTP instance‘);
return false;
}
}
function startRequest(){
createXmlHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","parsetXML.xml",true);
xmlHttp.send(null);
}
function handStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
listAllStates();
}
}
}
function listAllStates(){
//xmlHttp.responseXML得到服务器的响应,表示为XML,这个对象可以解析为一个DOM对象
var xmlDoc=xmlHttp.responseXML;
//返回所有是state标记名的元素数组
var allStates=xmlDoc.getElementsByTagName("state");
outputList("All States in Document",allStates);
}
function outputList(title,states){
var out=title;
var currentState=null;
for(var i=0;i<states.length;i++){
currentState=states[i];
out=out+"\n"+currentState.childNodes[0].nodeValue;
}
alert(out);
}
</script>
</head>
<body>
<form>
<input type="button" value="view all Listed States" onClick="startRequest()"/>
</form>
</body>
</html>
下面的getAndPostExample.jsp中展示了get,post方法像服务器发送数据
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>Sending Request Data Using GET and POST</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if (window.XMLHttpRequest) { // Mozilla, Safari,...
xmlHttp= new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType(‘text/xml‘);
}
} else if (window.ActiveXObject) { // IE
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlHttp) {
alert(‘Giving up :( Cannot create an XMLHTTP instance‘);
return false;
}
}
function createQueryString(){
var firstName=document.getElementById("firstName").value;
var middleName=document.getElementById("middleName").value;
var birthday=document.getElementById("birthday").value;
var queryString="firstName="+firstName+"&middleName="+middleName+"&birthday="+birthday;
return queryString;
}
function doRequestUsingGET(){
createXMLHttpRequest();
var query="GetAndPostExample.do";
//因为是在struts中,url可以为GetAndPostExample.do,但是如果在Servlet中,查询串会追加到请求url中的。当然即使我们在struts中,url追加了查询串(queryString),也是可以的,只是没有必要这么麻烦。
//query=query+"?"+createQueryString();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",query,true);
xmlHttp.send(null);
}
function doRequestUsingPOST(){
createXMLHttpRequest();
var url="GetAndPostExample.do";
var queryString=createQueryString();