Java 如何发送 XML 作为对来自 JSP 的 HTTP post 请求的响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19031969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send an XML as response to a HTTP post request from a JSP
提问by Raj
I need to create a jsp that returns (a small) xml as response to a HTTP POST request. I tried googling and found some pages in SO that accomplish this using servlets as follows:
我需要创建一个返回(一个小)xml 作为对 HTTP POST 请求的响应的 jsp。我尝试使用谷歌搜索并在 SO 中找到了一些使用 servlet 完成此操作的页面,如下所示:
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
and then writing the xml through the out
object. I couldn't find a way to do the same with JSP. Any pointers on how to do that in JSP will be really helpful.
然后通过out
对象写入xml 。我找不到对 JSP 执行相同操作的方法。有关如何在 JSP 中执行此操作的任何指示都将非常有帮助。
回答by developerwjk
This is very easy. And I'm mentioning the sacrosanct "you should never use Java code in a JSP" right here, so no need to downvote this answer for showing how to do what you believe should not be done.
这很容易。我在这里提到了神圣不可侵犯的“你永远不应该在 JSP 中使用 Java 代码”,所以没有必要贬低这个答案来展示如何做你认为不应该做的事情。
<%
response.setContentType("text/xml");
String somedata = "whatever";
out.print("\n<root>");
out.print("\n <othertag>" + somedata + "</othertag>");
out.print("\n</root>");
%>
Or:
或者:
<%
response.setContentType("text/xml");
String somedata = "whatever";
%>
<root>
<othertag><%=somedata%></othertag>
</root>