0

I'm a beginner who's studying Java and builds a website by JSF using the BootFaces. I'm now doing a project about a Travel Site so there are 2 types of users: Admin and Users. I encountered a problem when doing the project that after logging in succesfully, the welcomeRegularUser.xhtml and welcomeAdmin.xhtml appeared twice. For example, at the first time, the project page redirect from index page to http://localhost:8080/TravelWeb/ page which has welcomeRegularUser info. I click on the SearchAttraction button, it will now really go to the http://localhost:8080/TravelWeb/welcomeRegularUser.xhtml on the link. Now the button works correctly that calls the function searchAttraction(). I don't know why, but I hope someone can help me. Thanks

Login.java

@ManagedBean
@SessionScoped
public class Login implements Serializable {

//attributes

private String uid;

private String password;
private int type;
private RegularUser regularUserAccount;
private Admin adminAccount;

public Login() {
    regularUserAccount = null;
    adminAccount = null;
}

public String getUid() {
    return uid;
}

public String getPassword() {
    return password;
}

public int getType() {
    return type;
}

public RegularUser getRegularUserAccount() {
    return regularUserAccount;
}

public Admin getAdminAccount() {
    return adminAccount;
}

public void setType(int type) {
    this.type = type;
}

public void setUid(String uid) {
    this.uid = uid;
}

public void setPassword(String password) {
    this.password = password;
}



public String errorPassword() {    
    FacesMessages.error("Error!", "The <strong>Password</strong> is 
                 incorrect!");
    return null;
}
public String errorUsername() {
    FacesMessages.error("Error!", "The <strong>Username</strong> is 
          not in the System!");
    return null;
}
public String blockedUser() {
    FacesMessages.error("Error!", "The <strong>Username</strong> is 
                already blocked!");
    return null;
}

public String login() {

    if (uid.equals("admin") && password.equals("admin")) {
        adminAccount = new Admin(uid, password, type);
        return "welcomeAdmin";
    }
    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;

    try {
        conn = DatabaseUtil.getConnection();
        stat = conn.createStatement();
        rs = stat.executeQuery("Select * from BSN_User where uid = '" 
                   + uid + "'");

        if (rs.next()) {
            if (password.equals(rs.getString(2))) {
                if (rs.getInt(4) == 9) {
                    return blockedUser();
                }   
                regularUserAccount = new RegularUser(uid, password, 
                         rs.getString(3), type);
                return "welcomeRegularUser";
            } else {
                return errorPassword();

            }
        } else {
            return errorUsername();

        }

    } catch (SQLException e) {
        e.printStackTrace();
        return ("internalError");
    } finally {

        DatabaseUtil.closeConnection(conn);
        DatabaseUtil.closeStatement(stat);
        DatabaseUtil.closeResultSet(rs);
    }

}

index.xhtml

<h:head>
    <title>Travel Advisor Home Page</title>
    <style type="text/css">
        .form-signin{
            margin: 0 auto;
            width: 300px;
            padding:15px;
        }
    </style>
</h:head>
<h:body style="background-color: lightblue;">
    <b:container style="padding:100px;">
        <h:form styleClass="form-signin" prependId="false">
            <b:image value="../images/logo.png" style="height: 268px; 
                    width: 268px;"></b:image>

                <h2 class="form-signin-heading" style="text-align: 
                    center;">Login</h2>
                <b:row>

                    <b:inputText id="username" placeholder="Username"
                                 autocomplete="false"
                                 value="#{login.uid}" 
                     label="Username">

                    <f:facet name="prepend"><b:icon name="user"/> 
                 </f:facet> 
                    </b:inputText>

                </b:row>
                <b:row>

                    <b:inputSecret id="password" 
          placeholder="Password"
                                   autocomplete="false"
                            value="#{login.password}" 
label="Password">
                    <f:facet name="prepend"><b:iconAwesome 
name="key"/></f:facet>
                    </b:inputSecret>

                    <b:selectBooleanCheckbox value="false" 
    caption="Remember Me"></b:selectBooleanCheckbox>

                    <b:growl id="growl" globalOnly="true" 
     showDetail="true" severity="error"></b:growl>

                    <b:commandButton look="primary btn-block" 
      value="Login" icon="log-in" action="#{login.login()}" 
      update="growl"></b:commandButton>

                    <b:commandButton look="primary btn-block" 
       value="Register" icon="pencil" action="register.xhtml"> 
       </b:commandButton>

                </b:row>

        </h:form>
    </b:container>

</h:body>
Stephan Rauh
  • 3,069
  • 2
  • 18
  • 37
L.O
  • 9
  • 1
  • 1
    Does https://stackoverflow.com/questions/15521451/how-to-navigate-in-jsf-how-to-make-url-reflect-current-page-and-not-previous-o help? – Kukeltje Dec 03 '18 at 08:33
  • 1
    Off topic: you are allowing SQL injection. See https://stackoverflow.com/questions/9516625/prevent-sql-injection-attacks-in-a-java-program – Jasper de Vries Dec 03 '18 at 09:49
  • 1
    Off topic: it's a good idea to leave `prependId` at its default value. It's useful in special situations, but you may run into all kinds of problems if you're using a data table, a modal, tabs, and other widgets implementing the `NameSpace` class. – Stephan Rauh Dec 08 '18 at 19:52
  • At the moment, I can't spot the bug. Your souce code *should* work. Can you provide us a complete, simple reproducer? Ideally a GitHub project based on Maven that runs without further configuration? In particular, without a database, because it always takes a while to configure databases. – Stephan Rauh Dec 08 '18 at 19:54
  • @StephanRauh: from how I read the qyestion, I think it is 'the URL is one behind' issue like I referres to in my first comment – Kukeltje Dec 08 '18 at 20:22
  • @ L.O Did you solve the problem? Did one of the hints help you? If so, I'd like to add a proper answer so other users benefit from your experience, and I'd like to edit the answer because @Kukeltje and I read it differently. Or rather, I'd prefer you to edit the question and to answer it yourself. – Stephan Rauh Dec 08 '18 at 21:44
  • Since how I read it (but I might be wrong), it is a duplicate I coud vote as such so no need to answer. But OP seems a bit quiet... – Kukeltje Dec 09 '18 at 11:26

0 Answers0