1.Response Text
1.1 JSP
Client:
function JSP_Text(){
var url = server.jsp";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET",url);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){ alert(xmlHttp.responseText);
}
}
};
xmlHttp.send(null);
}Server:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%
System.out.println("Server response");
out.println(request.getParameter("Hello!"));
%>
1.2 Servlet
Client:
function Servlet_Text(){
var url = /server?";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET",url);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){ alert(xmlHttp.responseText);
}
}
};
xmlHttp.send(null);
}Server:
/**
* Get响应
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Document document = DocumentHelper.createDocument();
Element map = document.addElement("map");
map.setText("This is a map");
try{ response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.write(obj.toString());
}catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
2. Response XML
2.1 JSP
Client:
function JSP_XML(){
var url = "server.jsp";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET",url);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
listXmlNodes();
}
}
};
xmlHttp.send(null);
}
function listXmlNodes(){
var xmlDoc=xmlHttp.responseXML;
var mapNode=xmlDoc.getElementsByTagName("map")[0];
alert(mapNode.childNodes[0].nodeValue);
} Servlet:
<%
try{
Document document = DocumentHelper.createDocument();
Element map = document.addElement("map");
map.setText("This is a map");
response.setContentType ("text/xml");
PrintWriter writer = response.getWriter();
//输出方法一
//document.writer(writer);
//输出方法二
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter output = new XMLWriter(writer,format);
output.write(document);
output.close();
}catch(Exception e){
System.out.println(e);
}
%>
2.2 Servlet
Client:
function Servlet_XML(){
var url = "/server?";
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET",url);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
listXmlNodes();
}
}
};
xmlHttp.send(null);
}
function listXmlNodes(){
var xmlDoc=xmlHttp.responseXML;
var mapNode=xmlDoc.getElementsByTagName("map")[0];
alert(mapNode.childNodes[0].nodeValue);
}Server:
/**
* Get响应
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Document document = DocumentHelper.createDocument();
Element map = document.addElement("map");
map.setText("This is a map");
try {
response.setContentType("application/xml");
PrintWriter writer = response.getWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter output = new XMLWriter(writer, format);
output.write(doc);
output.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
需要注意的是这里的ContentTyp要设定成"application/xml",否则客户端无法得到响应




浙公网安备 33010602011771号