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>