HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(portletResponse);
ELContext elContext = facesContext.getELContext();
// Invoke the Liferay HtmlTopTag class directly (rather than using liferay-util:html-top from a JSP).
StringJspWriter stringJspWriter = new StringJspWriter();
BodyContent bodyContent = new StringBodyContent(stringJspWriter);
String elementAsString = element.toString();
HtmlTopTag htmlTopTag = new HtmlTopTag();
PageContextAdapter pageContextAdapter = new PageContextAdapter(httpServletRequest, httpServletResponse,
elContext, stringJspWriter);
htmlTopTag.setPageContext(pageContextAdapter);
htmlTopTag.doStartTag();
bodyContent.print(elementAsString);
htmlTopTag.setBodyContent(bodyContent);
try {
htmlTopTag.doEndTag();
}