Jan Aren� wrote:
> Hi
>
> I have a problem regarding a XML response. One of our clients need to get a
> response from our Web server in XML format, and I know "nothing" about XML.
> Ok, I know some teori but not so I could do something useful.
>
> What I need is to generate a response message like this
> (in their words: "The response from your server could look like this")
>
> <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
> <messages>
> <message id="1" to="123456" from="654321" msg="My message"
> status="1"/>
> </messages>
>
> I guess I should generate the String and send it back as a response somehow,
> instead of the ordinary HTTP response?

JSP is not tied to HTML (or any specific text format), so you can use
any text as the "template data" in the page, for instance the XML
elements your client wants. Just a couple of things to think about:
1) Some XML clients are picky about the XML declaration being the
    very first thing in the document, and may be turned off even by
    a single whitespace ahead of the declaration. You should therefore
    put "<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>"
    as the very first thing in the JSP page.
2) You most likely need to use a different content-type for the
    response than the default "text/html"; set it with the page
    directive "contentType" attribute.

With this in mind, a JSP page that generates the XML sample you
provided could look something like this (assuming the data for the
message elements is available in a scoped variable named "messages")
using JSTL:

   <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
   <%@ page contentType="text/xml" %>
   <%@ taglib prefix="c"uri="http://java.sun.com/jstl/core"%>

   <messages>
     <c:forEach items="${messages}" var="m">
       <message id="<c:out value="${m.id} />"
         to="<c:out value="${m.to}" />"
         from="<c:out value="${m.from}" />"
         msg="<c:out value="${m.msg}" />"
         status="<c:out value="${m.status}" />"
       />
     </c:forEach>
   </messages>

For more about JSTL, see <http://java.sun.com/products/jsp/jstl/>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to