tags:

views:

595

answers:

8

Hello everybody,

I need to send xml to the browser with my jsf application. This xml is generated by the application. I try to create it but my jsf application sends html every time.

How can I change the content-type to send xml ?

Thanks for your help.

A: 

I would have a servlet return XML rather than the JSF stuff. I take JSF (and JSPs and any other UI layer) as returning "human readable" content. XML is machine readable.

RE: Session data - I've never done this, not used JSF just know it as a UI layer, but a quick google returns: http://blogs.sun.com/chrisf/entry/retrieving%5Fjsf%5Fsession%5Fvariables%5Fin

mlk
A: 

For Montecristo :

I try to use it but it doesn't work.

For mlk:

If I must use another servlet, how can I access to the jsf session data with the user connected and other things ?

Kiva
A: 

You need to specify the content-type (sec. 14.17) of the returning page as text/xml. It'll currently be set as text/html.

Browsers have used a variety of mechanisms to determine the type of the data being returned, but the proper, most reliable way is to set the HTTP Content-type header.

I'm sorry - I'm not JSF knowledgeable, so I can't advise on how to fix this, but it gives you something to look for now!

Brian Agnew
Yes I would like to set this content-type but I don't know to do this in JSF page.
Kiva
A: 

I already searched in this way before to post here but I didn't found in google how to set the content-type in jsf.

It's for this reason that I post here ;)

Kiva
+1  A: 

You can set the content-type within your JSP. I assume you are using a JSP and creating the xml content from a backing bean? A JSP like this would output XML:

<%@page contentType="text/xml"%><?xml version="1.0" encoding="UTF-8"?>
<portfolio>
  <stock>
    <symbol>SUNW</symbol>
    <name>Sun Microsystems</name>
    <price>17.1</price>
  </stock>
  <stock>
    <symbol>AOL</symbol>
    <name>America Online</name>
    <price>51.05</price>
  </stock>
  <stock>
    <symbol>IBM</symbol>
    <name>International Business
    Machines</name>
    <price>116.10</price>
  </stock>
  <stock>
    <symbol>MOT</symbol>
    <name>MOTOROLA</name>
    <price>15.20</price>
  </stock>
</portfolio>

You could then easily change these hardcoded values to be bean values from your backing-bean in the way you would normally do for HTML-outputting JSPs.

Steve Claridge
Ok I'll try this ;)
Kiva
+1  A: 

There are several ways to do this. Doing it in JSP is a bit nasty.

As already mentioned you can use a Servlet and inject/load your variables in there. Eg by accessing the session context:

MyBean myBean = (MyBean)FacesContext.getCurrentInstance()
                         .getExternalContext().getSessionMap().get("myBean");

Or you can output it to the HTTP Response from a method in your Backing Bean. Eg:

try {
    String xml = "<person>damian</person>";
    FacesContext ctx = FacesContext.getCurrentInstance();
    final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();

    resp.setContentType("text/xml");
    resp.setContentLength(xml.length());
    resp.getOutputStream().write(xml.getBytes());
    resp.getOutputStream().flush();
    resp.getOutputStream().close();

    ctx.responseComplete();

} catch (IOException e) {
    e.printStackTrace();
}

Or if you are using Facelets you can set the response type in the <f:view> tag.

Damo
Why is the JSP tag "nasty"? If you have a page that delivers XML then it seems like the logical thing to do?I guess it boils down to whether he wants to output an XML stream as in your example or whether he wants to define the XML structure in the JSP and use his backing-bean to fill in the data elements.
Steve Claridge
True. There may be some situations where rendering it on the page could be "useful" but IMO it's usually nasty. Same goes for doing it via Facelets or any other "view" framework.
Damo
A: 

Thanks damo, I'll try asap.

As I use facelet, I'll try your second option ;)

Kiva
A: 

Thanks a lot Damo, it's working now.

Kiva