主题:在JSP中用AJAX时,怎样写服务器端将数据库读出的内容组装成XML,然后怎样传回前台
holly2k
[专家分:120] 发布于 2007-10-23 15:20:00
如题,如果能有一段代码来看看最好啦
回复列表 (共3个回复)
沙发
yexin218 [专家分:4070] 发布于 2007-10-24 22:42:00
[color=FF0000]
httpServletResponse.setContentType("text/xml; charset=GB2312");
httpServletResponse.setHeader("Cache-Control", "no-cache");
//according to the result,if address of songs exists then output it
String content="";
System.out.println("*******搜索结果********\n");
if(getFoundFlag()){
//setFoundFlag(false);
System.out.println("@@@@@@@@找到@@@@@@@@@@@");
content = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
+ "<baidu><address><song_addr>"
+ getRealSongAddr(this.getSongAddr(), this.getSongName())
+ "</song_addr>" + "<word_addr>";
if(getFoundWordFlag()){
content+=getRemoteFileContent(getRealWordAddr(this.getWordAddr()));
}else{
content+=this.getWordAddr();
}
content+= "</word_addr><message>Congratulations,we get it...</message></address></baidu>";
}else{
System.out.println("@@@@@@@@没有找到@@@@@@@");
content = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"+"<error><warning><message>Sorry,we didn't get what your need..</message></warning></error>";
}
System.out.println(content);
System.out.println("-------End-------------\n");
httpServletResponse.getWriter().write(content);[/color]
我用servlet返回结果的
板凳
holly2k [专家分:120] 发布于 2007-10-24 23:39:00
不错,这个答案还过得去,就是写了很多不知道的东西,不过还算看得懂,最后问一下,那个httpServletResponse.getWriter().write(content);
改成httpServletResponse.getWriter().println(content);
行不行啊
3 楼
holly2k [专家分:120] 发布于 2007-10-25 23:51:00
不知道为什么我的Servlet这么写结果老是说xml只能为一个根元素
String result = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n";
result += "<people>";
while(rs.next()) {
result += "<person>";
result += "<NO>" + rs.getString(1) + "</NO>\n";
result += "<NAME>" + rs.getString(2) + "</NAME>\n";
result += "</person>";
}
result += "</people>";
out.println(result);
out.write(result) ;
我来回复