I am starting to learn JSF and I am not sure if this is the right approach, but what I want to do is allow a user to login and validate from a database and then have a welcome message with the user's first name e.g. Welcome Bob. The database will have all the user info and login will consist of only the email and password.
Currently I have a ManagedBean with all the getters and setters plus a validation method, which calls a method in the DAO.
@Named(value = "custBean")
@SessionScoped
public class CustomerManagedBean implements Serializable {
/**
* Creates a new instance of CustomerManagedBean
*/
public CustomerManagedBean() {
}
private int custId;
private String firstname;
private String lastname;
private String email;
private String password;
private String address;
private String city;
private String state;
private int zip;
public int getCustId() {
return custId;
}
public void setCustid(int custId) {
this.custId = custId;
}
// More getters/setters here, not shown
public String validateEmailPassword() {
boolean valid = LoginDAO.validate(email, password);
if (valid) {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false);
session.setAttribute("email", email);
session.setAttribute("firstname", firstname); // I can't set this because user did not input this on login so how do I set it from database?
session.setAttribute("lastname", lastname); // and this
session.setAttribute("address", address); // and this
session.setAttribute("state", state); // and this
session.setAttribute("zip", zip); // and this
return "index";
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect Email and Password",
"Please enter correct Email and Password"));
return "login";
}
}
Login.xhtml is very simple
<h:form>
<h3>Login</h3>
<h:outputText value="Username" />
<h:inputText id="username" value="#{custBean.email}"></h:inputText>
<h:message for="username"></h:message>
<br></br>
<h:outputText value="Password" />
<h:inputSecret id="password" value="#{custBean.password}"></h:inputSecret>
<h:message for="password"></h:message>
<br></br>
<h:commandButton action="#{custBean.validateEmailPassword}" value="Login"></h:commandButton>
</h:form>
Am I correct in that the bean gets initialized in the login.xhtml by user input on #{custBean.email} and #{custBean.password}? So can I set that bean's other variables after a database call? I hope I am making sense, but what I am able to do is after I login, I'm able to have #{custBean.email} displayed. It's just the firstname and the others I'm not able to show.
Here's my validate method in my DAO.
public static boolean validate(String email, String password) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DataConnect.getConnection();
ps = con.prepareStatement("SELECT * FROM customer WHERE email = ? AND password = ?");
ps.setString(1, email);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return true;
}
} catch (SQLException ex) {
System.out.println("Login error -->" + ex.getMessage());
return false;
} finally {
DataConnect.close(con);
}
return false;
}
UPDATE I added an Customer entity from database and a CustomerController along with a generic AbstractFacade and CustomerFacade generated by Netbeans. My CustomerController is below. Is this a better way to interact with the DB?
@Named(value = "customerController")
@SessionScoped
public class CustomerController implements Serializable {
@EJB
CustomerFacade custFacade;
@Inject
CustomerManagedBean custBean;
public CustomerController() {
}
public String validateCustomer() {
Customer c = new Customer();
c = custFacade.getValidUser(custBean);
if (c != null) {
custBean.setEmail(c.getEmail());
custBean.setFirstname(c.getFirstname());
custBean.setLastname(c.getLastname());
custBean.setAddress(c.getAddress());
custBean.setCity(c.getCity());
custBean.setState(c.getState());
custBean.setZip(c.getZip());
}
}