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 AjaxServlet extends HttpServlet {
public AjaxServlet() {
super ();
}
public void destroy() {
super .destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 这里响应类型必须是text/xml
response.setContentType( "text/xml;charset=gbk" );
request.setCharacterEncoding( "gbk" );
PrintWriter out = response.getWriter();
// XML头,没有这个可能Javascript解析不出XML,最好设一下
StringBuilder sb = new StringBuilder( "<?xml version=\"1.0\" encoding=\"gbk\"?>" );
// 根元素<root></root>一定需要,否则Javascript解析不出XML,但名字不一定要叫root
sb.append( "<root><info id=‘1‘><b>a</b></info ><info id=‘1‘>b</info><memo id=‘2‘>c</memo><memo>d</memo></root>" );
//写出XML
out.println(sb.toString());
out.flush();
out.close();
}
public void init() throws ServletException {}
}
|