I am new to JSF and have a login page. Here it is:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputText value="User:"></h:outputText>
<h:inputText id="username" value="#{login.username}"></h:inputText>
<h:outputText value="Password"></h:outputText>
<h:inputSecret id="password" value="#{login.password}"></h:inputSecret>
<h:commandButton value="Login" action="#{login.checkDB}"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
And here is the Login jsf page
@Named(value = "login")
@Dependent
@RequestScoped
public class Login{
private String username = "", password = "";
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUsername(String Username) {
username = Username;
}
public void setPassword(String Password) {
password = Password;
}
public void checkDB() {
try {
con=DB.connect();
PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
"SELECT * FROM Users where username = ? AND password = ?");
checkDB.setString(1, username);
checkDB.setString(2, password);
ResultSet rs = null;
rs = (ResultSet) checkDB.executeQuery();
if (rs.next()) {
User.customer = new Customer(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7));
rs.close();
DB.disconnect(con);
FacesContext.getCurrentInstance().getExternalContext().redirect("main.xhtml");
} else {
rs.close();
DB.disconnect(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And here is the User class:
@ManagedBean(name = "user")
@SessionScoped
public class User implements Serializable {
public static Customer customer;
private Connection con=null;
public void setCustomer(Customer customer) {
User.customer = customer;
}
public Customer getCustomer() {
return customer;
}
public User() {
}
public void editInfo() {
String name = customer.getName(), surname = customer.getSurname(), password = customer.getPassword();
String phone = customer.getPhone(), address = customer.getAddress(), email = customer.getEmail();
try {
con=DBConnect.connect();
int result = -1;
PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
"UPDATE users set password=?,name=?,surname=?,phone=?,address=?,"
+ "email=? where username=?");
checkDB.setString(7, customer.getUsername());
checkDB.setString(1, password);
checkDB.setString(2, name);
checkDB.setString(3, surname);
checkDB.setString(4, phone);
checkDB.setString(5, address);
checkDB.setString(6, email);
result = checkDB.executeUpdate();
con.close();
FacesContext.getCurrentInstance().getExternalContext().redirect("editsuccesfull.xhtml");
} catch (Exception e) {
e.printStackTrace();
}
}
The problem is, after username and password are set by the setter functions, they sudenly become null when the function returns from the managed bean. Why is this happening? How can i fix it?