If you're using <h:head>
in JSF2, then you can in theory do this by explicitly flushing the underlying servlet output stream in the encodeEnd()
method of the Renderer
associated with the component.
There are basically two ways to implement a custom renderer. If you want to be implementation independent, then you have write the entire Renderer
yourself which is a piece of work (although for a simple HTML <head>
element this is not that hard). You can also choose to be implementation specific so that you end up with a simpler custom Renderer
implementation. Assuming that you're using Mojarra, then you just have to extend the com.sun.faces.renderkit.html_basic.HeadRenderer
.
E.g.
package com.example;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import com.sun.faces.renderkit.html_basic.HeadRenderer;
public class FlushedHeadRenderer extends HeadRenderer {
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
super.encodeEnd(context, component);
context.getExternalContext().getResponseOutputWriter().flush(); // Forces flush.
}
}
which you then register as follows in faces-config.xml
:
<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>javax.faces.Head</renderer-type>
<renderer-class>com.example.FlushedHeadRenderer</renderer-class>
</renderer>
</render-kit>
However, this approach has caveats. JSF does not have the opportunity anymore to create a session whenever necessary, or to handle any exception as error page whenever an exception is been thrown in midst of rendering the view. I would not recommend this approach for JSF.