FacesContext facesContext = FacesContext.getCurrentInstance(); String message = "Welcome " + username + "!"; facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
public void redirect(String url) throws Exception { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.redirect(url); }In this example, we use FacesContext.getCurrentInstance() to get the current instance of the FacesContext. We then get the ExternalContext through the FacesContext, and use it to redirect the user to a different URL. The javax.faces.context package from the Java Server Faces (JSF) library contains FacesContext and other classes related to the FacesContext.