jsp 中输出 xml文(和用ie直接打开xml一样效果)

<%
 request.setCharacterEncoding("GBK");
 String xmlStr = request.getAttribute("xmlStr").toString();
 if(xmlStr != null && !"".equals(xmlStr)) { 
  response.setContentType("text/xml;charset=GBK"); 
  response.getWriter().write(xmlStr);
  response.flushBuffer();
  response.setContentType("application/octet-stream");
  response.addHeader("Content-Disposition","inline;filename=1.xml");
 } 
%>

  注:request.setCharacterEncoding("GBK");  // 中文显示不是乱码

  在jsp页面除了jsp代码,不能写任何非jsp代码,否则打开页面时,会报xml格式错误,无法显示。

 当取出来的xml为null或空时,需要用js来提示用户,可以参考如下代码(我用的是EXtjs):

<%
	request.setCharacterEncoding("GBK");
	String xmlStr = request.getAttribute("xmlStr").toString();
	if(xmlStr != null && !"".equals(xmlStr)) { 
		
		response.setContentType("text/xml;charset=GBK"); 
		response.getWriter().write(xmlStr);
		response.flushBuffer();
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition","inline;filename=1.xml");
	} else {
%>
<%@ page contentType="text/html;charset=GBK"%>
<%		
		 response.setContentType("text/html;charset=GBK"); 
		 String html = ""
		 + "<HTML><HEAD><meta http-equiv='Content-Type' content='text/html; charset=GBK'>"
		 + "<link rel='stylesheet' type='text/css' href='../resources/css/ext-all.css'/>"
		 + "<script type='text/javascript' src='../javascript/Ext/ext-base.js'></script>"
		 + "<script type='text/javascript' src='../javascript/Ext/ext-all.js'></script>"
		 + "<script type='text/javascript' src='../javascript/Ext/ext-lang-zh_CN.js'></script>"
		 + "<script type='text/javascript'>"
		 + "Ext.onReady(function(){"
		 + "Ext.MessageBox.buttonText.yes='是';"	 
		 + "Ext.MessageBox.buttonText.no='否';"	 
		 + "Ext.Msg.confirm('提示','没查询到结果,是否关闭?',function(btn){"
		 + "if(btn == 'yes') {"
		 + "window.close();"
		 + "}"
		 + "});"
		 + "});"
		 + "</script>"
		 + "</HEAD>"
		 + "</HTML>";
		 //out.println(html);
		 response.getWriter().write(html);
	}
%>

  

你可能感兴趣的:(JavaScript,xml,jsp,IE,ext)