How-to get the JSF application context path?
195661Oct 7 2005 — edited Oct 17 2005I want to do a redirect from within a JSF backing bean. How do I get the application context path?
I plan to do this in the constructor of the backing beans in order to validate the user has previously logged in.
public BackingBeanName()
{
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = HttpSession)facesContext.getExternalContext().getSession(false);
HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest();
userid = (String)session.getAttribute("userid");
if (userid == null) {
logger.info("userid attribute is not in session");
ExternalContext ec = facesContext.getExternalContext();
appContextPath = ???????????????????????;
String redirectTo = appContextPath + "/logon.jsp";
try {
ec.redirect(redirectTo);
}
catch (IOException e) {
String msg = "redirect to " + redirectTo + " failed";
logger.severe(msg);
return; // throw an exception instead of return;
}
}
}
Regards,
Al Malin