0

I need to show output in a jsf page that is not formatted as html (without header and without html tags), but as a simple text file. This is possible with JSF 2.0 or I necessarily need a servlet? Thanks

EDIT: a client makes a request through url (with parameters) and I have to give it a response. I know that I can use a servlet for this but wanted to know if it was possible to use a Bean/JSF instead. the problem is that I have to give response that can not be an html file but a text file (for simple parsing), but that should not be downloaded but displayed directly in the browser. I hope I was clear

noiseimpera
  • 35
  • 1
  • 2
  • 10
  • Remove all the tags from .xhtml file and it will print plain text. – Makky Jul 03 '13 at 08:48
  • I do not have to do it manually. The client makes a request and I have to give to it a response which should not be html pages but text file. This text file will contain processing done by the backing bean java. – noiseimpera Jul 03 '13 at 08:55
  • as far as i understand i understand html content does not needed. you need to send a text file to client like downloading a text file? – erencan Jul 03 '13 at 08:58
  • Can you explain a bit more with some example your question is not self explain what exact you achieve here? – Subodh Joshi Jul 03 '13 at 09:01
  • Its better to use Servlet. – Makky Jul 03 '13 at 09:22

3 Answers3

2

I know that I can use a servlet for this but wanted to know if it was possible to use a Bean/JSF instead.

Yes, it's quite possible with JSF as well. The entire Facelet page can look like this:

<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:event type="preRenderView" listener="#{bean.renderText}" />
</ui:composition>

And the relevant method of the bean can look like this:

public void rendertext() throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    Map<String, String> params = ec.getRequestParameterMap();
    String foo = params.get("foo"); // Returns request parameter with name "foo".
    // ...

    ec.setResponseContentType("text/plain");
    ec.setResponseCharacterEncoding("UTF-8");
    ec.getResponseOutputWriter().write("Some text content");
    // ...

    fc.responseComplete(); // Important! Prevents JSF from proceeding to render HTML.
}

However, you're then essentially abusing JSF as wrong tool for the purpose. JSF adds too much overhead in this specific case which you don't need at all. A servlet is then much better. You can use the @WebServlet annotation to register it without any need for XML configuration. You also don't need a Facelet file anymore.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

You can use Java Servlet to output response in plain text.

Example :

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Content-Type", "text/plain");
    response.setHeader("success", "yes");
    PrintWriter writer = response.getWriter();
    writer.write("This is plain response\n");
    writer.close();
}
Makky
  • 17,117
  • 17
  • 63
  • 86
0

JSF only renders HTML if you use components that generate such content. You can produce text/html content like this:

<f:view xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    contentType="text/plain"
    encoding="UTF-8">
    <ui:composition>
        Your plain text goes here.
        You can use expressions as usual: #{myBean.value}.
    </ui:composition>
</f:view>

Just plain text will be rendered. With the f:view component attributes you set the response headers.

whbogado
  • 929
  • 5
  • 15