有人可以帮我解决这个错误吗?它在 UserDao.login 和 LoginBean.loginproject 处显示 NullPointerException。我是 JSF 2 中过滤器的新手。方法 session.getAttribute("attribute") 中使用了哪个属性?
非常感谢你
问候
用户道.java
public class UserDAO {
...
public static Connection getInstance(){
if(connection == null)
new UserDAO();
return connection;
}
public static boolean login(String user, String password) throws SQLException {
connection = getInstance();
String Query1 = "SELECT nama, password " + "FROM user " + "WHERE nama = ? AND password = ?";
PreparedStatement statement1 = (PreparedStatement) connection.prepareStatement(Query1);
statement1.setString(1, user);
statement1.setString(2, password);
ResultSet rs = statement1.executeQuery();
if (rs.next()) // found
{
System.out.println(rs.getString("nama"));
return true;
}
else {
return false;
}
}
}
登录Bean.java
public class LoginBean implements Serializable {
...
//getters setters
public String loginProject() throws SQLException {
boolean result = UserDAO.login(uname, password);
if (result) {
// get Http Session and store username
HttpSession session = Util.getSession();
session.setAttribute("username", uname);
return "home";
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Invalid Login!",
"Please Try Again!"));
return "login";
}
}
public String logout() {
HttpSession session = Util.getSession();
session.invalidate();
return "login";
}
}