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.οnlοad=function(){
	var bt=document.getElementById("bt");
	bt.οnclick=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);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值