Ajax——响应内容为xml

响应内容为xml数据
* 服务器端:
> 设置响应头:ContentType,其值为:text/xml;charset=utf-8
* 客户端:

> var doc = xmlHttp.responseXML;//得到的是Document对象!

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getcontextpath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajax5.jsp' starting page</title>
    
	<Meta http-equiv="pragma" content="no-cache">
	<Meta http-equiv="cache-control" content="no-cache">
	<Meta http-equiv="expires" content="0">    
	<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<Meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript">
function createXMLHttpRequest(){
	try {
		return new XMLHttpRequest();
	} catch (e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveObject("MicroSoft.XMLHTTP");
			} catch (e) {
				// Todo: handle exception
			}
		}
	}
}
window.onload=function(){
	var bt=document.getElementById("bt");
	bt.onclick=function(){
		//1.创建异步对象
		var xmlHttp=createXMLHttpRequest();
		//2.打开与服务器的连接
		xmlHttp.open("GET","<c:url value='/BServlet' />",true);
		//3.发送请求
		xmlHttp.send(null);
		//4.给onreadystatechange上注册监听器
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				var doc=xmlHttp.responseXML;
				// 查询文档下名为student的所有元素,得到数组,再取下标0元素
				var ele = doc.getElementsByTagName("student")[0];
				var number = ele.getAttribute("number");//获取元素名为number的属性值
				var name=ele.getElementsByTagName("name")[0].text;
				var age=ele.getElementsByTagName("age")[0].text;
				var sex=ele.getElementsByTagName("sex")[0].text;
				var text=number + "," + name + "," + age + "," + sex;
				var h1=document.getElementById("h1");
				h1.innerHTML=text;
		}
		};
	}
};
</script>
  </head>
  
  <body>
    <button id="bt">点击这里</button> 
    <h1 id="h1"></h1>
  </body>
</html>

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.servletexception;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BServlet extends HttpServlet {


	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws servletexception,IOException {

		response.setContentType("text/xml;charset=utf-8");
		
		String xml=("<students>"+
					"<student number='001'>"+
					"<name>ZhangSan</name>"+
					"<age>18</age>"+
					"<sex>mail</sex>"+
					"</student>"+
					"</students>");
		response.getWriter().print(xml);
	}

}

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...