-1

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:

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.

Raj
  • 4,342
  • 9
  • 40
  • 45
  • 2
    Don't do it in a JSP. – Sotirios Delimanolis Sep 26 '13 at 15:10
  • 1
    A JSP is essentially meant to be a view. If you're sending XML as the response, just set the ContentType to "text/xml" as above and print the XML. You shouldn't need a JSP to do that. – Prmths Sep 26 '13 at 15:14
  • Many recommend using application/xml as the content-type, not text/xml. See: http://stackoverflow.com/questions/3272534/what-content-type-value-should-i-send-for-my-xml-sitemap – John Sep 28 '13 at 14:16

1 Answers1

2

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.

 <%
 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>
developerwjk
  • 8,619
  • 2
  • 17
  • 33