This is possible with a custom UIComponent . My colleague wrote a blog article about this a year ago: Facelets and an inherited JSP .
This is some kind of code, but the principle is simple, the component executes RequestDispatcher#include() with a custom HttpServletResponseWrapper , which captures the written output and then writes it to the body of the JSF component. Here are excerpts of relevance:
Create class com.example.component.JspIncludeComponent
public class JSPIncludeComponent extends UIComponentBase { public String getFamily() { return "components.jsp.include"; } public void encodeBegin(FacesContext context) throws IOException { try { ExternalContext externalContext = context.getExternalContext(); HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
Create class com.example.CharResponseWrapper
public class CharResponseWrapper extends HttpServletResponseWrapper { private CharArrayWriter output; @Override public String toString() { return output.toString(); } public CharResponseWrapper(HttpServletResponse response) { super(response); output = new CharArrayWriter(); } public CharArrayWriter getCharWriter() { return output; } @Override public PrintWriter getWriter() { return new PrintWriter(output); } @Override public ServletOutputStream getOutputStream() { return new CharOutputStream(output); } public InputStream getInputStream() { return new ByteArrayInputStream( toString().getBytes() ); } } class CharOutputStream extends ServletOutputStream { private Writer output; public CharOutputStream( Writer writer ) { output = writer; } @Override public void write(int b) throws IOException { output.write(b); } }
Add to faces-config.xml
<component> <component-type>com.example.component.JSPIncludeComponent</component-type> <component-class>com.example.component.JSPIncludeComponent</component-class> </component>
Create my.taglib.xml (Facelet taglib) file in the WEB-INF folder
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib> <namespace>http://example.com/jsf</namespace> <tag> <tag-name>include</tag-name> <component> <component-type>com.example.component.JSPIncludeComponent</component-type> </component> </tag> </facelet-taglib>
Add to web.xml (as described in http://docs.oracle.com/javaee/6/tutorial/doc/bnawn.html )
<context-param> <param-name>javax.faces.FACELETS_LIBRARIES</param-name> <param-value>/WEB-INF/my.taglib.xml</param-value> </context-param>
So you can use it as
<ui:component xmlns:my="http://example.com/jsf" xmlns:ui="http://java.sun.com/jsf/facelets" > <my:include page="page.jsp" /> </ui:composition>
Last but not least, mark his last words
I would not recommend using this as a long-term solution, but it could facilitate the transition from the old JSP with smelly scriptlets and all of them to a more reasonable and modern Facelets application.