Reputation: 12876
I want to do a server-side forward from a JSF web app to a non-JSF web app, deployed in the same EAR. Is there a way to do this? It does not appear as though I can simply do this via the FacesContext. (Note, for more context, I am going to put data in the request object before the forward)
Upvotes: 1
Views: 1039
Reputation: 1109132
This is not available by any of the JSF APIs. You'd need to fall back to the raw Servlet API.
First of all you need to configure the server to enable the "cross context" option in order to be able to obtain the other servlet context by ServletContext#getContext()
. This is by default disabled on most servers. It's unclear which server you're using, so here are just some hints: for Tomcat, check the crossContext
attribute of context.xml
and for Glassfish, check the crossContextAllowed
property of glassfish-web.xml
.
Once done that, then you'll be able to obtain the other ServletContext
by its context path in JSF as follows:
ServletContext currentServletContext = (ServletContext) externalContext.getContext();
ServletContext otherServletContext = currentServletContext.getContext("/otherContextPath");
Then, you need to obtain a RequestDispatcher
from it on the path of the desired resource and invoke the forward()
on it hereby passing the current HTTP servlet request and response.
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
otherServletContext.getRequestDispatcher("/path/to/other.xhtml").forward(request, response);
Finally, you need to inform JSF that rendering of the response is been taken over and that JSF doesn't need to do that.
facesContext.responseComplete();
You only need to make sure that this action isn't been invoked by an ajax request, but by a normal synchronous request, simply because you're fully overtaking the control over rendering the response from JSF and this way the JSF JS/Ajax engine has totally no notion that a non-XML response would be returned.
(disclaimer: I've never used this in combination with JSF, but it works that way with "plain" JSP/Servlet and should theoretically work as good in a JSF webapp as that's just built on top of Servlet API)
Upvotes: 1