If you're using JSF-2, you can make use of implicit navigation:
public String action() {
if (password1 != null && password2 != null && password1.equals(password2)) {
return "connect";
} else {
FacesMessage msg = new FacesMessage("Passwords do not match");
FacesContext.getCurrentInstance().addMessage(null, msg);
return null;
}
}
This will navigate to a page connect.xhtml
if both passwords are equal. If they are not, the registration page will be re-rendered. To display the message, you need to add
<h:form>
<h:messages globalOnly="true" />
<h:inputText value="#{account.password1}" />
<h:inputText value="#{account.password2}" />
<h:commandButton value="Register" action="#{account.action()}" />
</h:form>
to your page.
See also:
Creating FacesMessage in action method outside JSF conversion/validation mechanism?