I want to forward request to a non-JSF page from JSF action method. I am using below code in my JSF action :
public String submitUserResponse() {
// ...
parseResponse("foo.jsp", request, response);
// ...
return "nextpage";
}
private String parseResponse(String uri, HttpServletRequest request, HttpServletResponse response) {
if (uri != null) {
RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
dispatcher.forward(request, response);
return null;
}
// ...
return "xxxx";
}
The submitUserResponse()
action method is being called when user clicks the submit button from the JSF page and this method returns nextpage
string. Here the request forwards to next JSF page in normal flow. But in my requirement, I need to forward request to next non-JSF page. It is going, but it is displaying below exception in server.
java.lang.IllegalStateException: Cannot forward after response has been committed
I observed that code lines between parseResponse(...)
and return "nextpage";
are still being executed after forwarding my request using dispatched.forward(uri)
. Same thing happened with response.sendRedirect(url)
. How is this caused and how can I solve it?