4

I am currently doing a client-side redirect to get from a legacy servlet (old part of an application) to a JSF page (new part of an application). I would prefer to do a server-side redirect if possible, so that I may place items in the request that the JSF page can pick up. (there is a set of data that needs to be "handed off" between the legacy servlet and the JSF page and I prefer not to put these in a client-side redirect URL (as URL parameters) but instead do this on the server-side).

If there is a way to do a server-side redirect between a servlet (not the Faces servlet) and a JSF page, can you please show me how?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
BestPractices
  • 12,738
  • 29
  • 96
  • 140

1 Answers1

3

Just call RequestDispatcher#forward() the usual way. All servlets intercept on forwarded requests as well. You just need to make sure that the forward path matches the FacesServlet mapping. Assuming that you've mapped it on *.xhtml, this should do:

request.getRequestDispatcher("/page.xhtml").forward(request, response);

The page can if necessary be placed in /WEB-INF folder if you want to prevent the endusers from being able to open it directly without invoking the servlet first.

request.getRequestDispatcher("/WEB-INF/page.xhtml").forward(request, response);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I should have mentioned-- these are in different WARs and have different context roots. Will that be an issue? – BestPractices May 08 '12 at 13:24
  • If the server isn't configured to share the contexts (as by default), then yes, that will be a blocker. Otherwise, you'd just get the other context by `ServletContext#getContext()` and grab the request dispatcher from it instead. E.g. `getServletContext().getContext("otherAppName").getRequestDispathcer("/WEB-INF/page.xhtml").forward(request, response);` It's unclear what server you're using, but in case of for example Tomcat, check its documentation using keyword "crosscontext". – BalusC May 08 '12 at 13:30
  • Great thanks-- incidentally am using WebLogic 11g. Will give that a shot. Thanks – BestPractices May 08 '12 at 13:44