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
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.