1

How can I validate password with confirm password in login.jsp and only if equal, then redirect to success.jsp which displays Welcome #{beans.username}.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Shahir_Nerd
  • 21
  • 2
  • 2
  • 7

3 Answers3

3

Just implement a Validator the usual way. Validation should surely not be done in the action method as others suggest/imply. If validation fails, then by default the action method just won't be invoked and the same page will just be redisplayed with validation errors. You can pass the password field as <f:attribute> of the confirm password field so that the validator can get hold of its value for test.

Here's a kickoff example of the view:

<h:inputSecret id="password" binding="#{passwordComponent}" value="#{bean.password}" required="true" />
<h:message for="password" />

<h:inputSecret id="confirm" required="#{not empty password.value}">
    <f:validator validatorId="confirmPasswordValidator" />
    <f:attribute name="passwordComponent" value="#{passwordComponent}" />
</h:inputText>
<h:message for="confirm" />

and the validator:

@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        UIInput passwordComponent = (UIInput) component.getAttributes().get("passwordComponent");
        String password = (String) passwordComponent.getValue();
        String confirmPassword = (String) value;

        if (confirmPassword != null && !confirmPassword.equals(password)) {
            throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Confirm password is not the same as password", null));
        }
    }

}

The message will appear in <h:message for="confirm">.

Note that you only need to have the password as a property in the backing bean, not the confirm password.

private String password;

// Getter+setter.

In the action method you can just return the navigation case outcome the usual way:

public String login() {
    User user = userService.find(username, password); 

    if (user != null) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);
        return "success?faces-redirect=true";
    } else {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
            FacesMessage.SEVERITY_WARN, "Unknown login, please try again.", null));
        return null;
    }
}

Since JSF 2.0 there's no need for verbose navigation cases in faces-config.xml anymore as the other answer suggest.


Unrelated to the concrete problem, why are you still sticking to the legacy JSP view technology? This has been deprecated since JSF 2.0 in favor of Facelets. Please consider migrating JSP to Facelets. Then you'll be able to use among others the new JSF 2.0 <f:ajax> awesomeness and strong Facelets templating capabilities.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

You'll have to define a login function in your managed bean. This function should return a string which value will apply the navigation rule described in your faces-config.xml.

Your faces-config.xml will include:

<navigation-rule>
    <description>
       Login
    </description>
    <from-view-id>/Login.jsp</from-view-id>
    <navigation-case>
        <from-outcome>login_success</from-outcome>
        <to-view-id>/Success.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>login_failed</from-outcome>
        <to-view-id>/Login_failed.jsp</to-view-id>
    </navigation-case>
</navigation-rule>

Your Beans.java managed bean will include the login function:

public String login(){
    //Compare login and password against a DB, file, etc.

    if(entered_password.equals(stored_passwd){
        return "login_success";
    }else{
        return "login_failed";
    }
}

And finally, in your Login.jsp form you'll define a login button (plus the h:inputText for login and password values) similar to this:

<h:commandbutton value="LOGIN" action="#{Beans.login}" />

So, when the user presses the login button, the login() function will be executed and the redirection will be determined depending on its returning value.

jmrodrigg
  • 550
  • 6
  • 24
  • that is correct but i have some problem , i have three fields in jsf – Shahir_Nerd Oct 03 '12 at 07:51
  • 1.username 2. password 3. confirm password ..... so if two password are same only then it is showing login succes but still it is entering the succes.jsp with username – Shahir_Nerd Oct 03 '12 at 07:52
  • Let me see if I understood... if both `username` and `password` are correct, it redirects to succes.jsp. And also, if `password` and `confirm_password` are equal, it also displays a message in succes.jsp ? – jmrodrigg Oct 03 '12 at 07:56
  • if username is entered and password and confirm_password are not equal,still it is navigating to success.jsp – Shahir_Nerd Oct 03 '12 at 12:15
  • I don't understand your scenario. The main point is that you can check what you need (username, password1, and even password2) in your `login()` function and, depending on your needings, return the proper string according to your faces-config.xml declaration. – jmrodrigg Oct 03 '12 at 13:24
0

as your password would be of type String. you can use String.equals() method to check if they are both equal.

   String pass1 ="#s27zaiyt0";
   String pass2 ="#s27zaiyt0";
   sysout(pass1.equals(pass2));
PermGenError
  • 43,905
  • 8
  • 81
  • 104