我在Servlet类中有此processRequest(..)
方法,其中我正在使用Firebase的isLoginSuccessful(..)
以ChildEventListener
方法检查用户的电子邮件和密码,并希望将用户重定向到home.jsp如果为true,则返回index.jsp,如果为false。但是此loginSuccess
方法中的布尔值isLoginSuccessful(..)
返回false
,因此重定向回index.jsp然后返回true
。意味着isLoginSuccessful(..)
被打断。该如何解决?
processRequest()方法:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
// initialize firebaseapp
FirebaseAppProvider.getFirebaseApp(getServletContext());
// email password from textbox
String user_email = request.getParameter("loginEmail");
String user_password = request.getParameter("loginPassword");
boolean loginSuccessful = isLoginSuccessful(user_email, user_password, response);
if (loginSuccessful) {
System.out.println("login if");
response.sendRedirect("home.jsp");
} else {
System.out.println("login else");
out.println("Login Failed");
response.sendRedirect("index.jsp#download");
}
}
isLoginSuccessful()方法:
boolean loginSuccess = false;
private boolean isLoginSuccessful(String userEmail, String userPassword, HttpServletResponse response) {
FirebaseDatabase dbFire = FirebaseDatabase.getInstance();
DatabaseReference dbRefUsers = dbFire.getReference().child("Users");
System.out.println("isLoginSuccessful method called");
dbRefUsers.orderByChild("user_email").equalTo(userEmail).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot ds, String string) {
System.out.println("on Child added called");
User user = ds.getValue(User.class);
boolean pwTrue = (userPassword.equals(user.getUser_password()));
if (pwTrue) {
System.out.println("if onChildAdded called");
loginSuccess =true;
System.out.println("loginSuccess = " + loginSuccess);
} else {
System.out.println("else onChildAdded called");
loginSuccess = false;
}
}
@Override
public void onChildChanged(DataSnapshot ds, String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onChildRemoved(DataSnapshot ds) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onChildMoved(DataSnapshot ds, String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onCancelled(DatabaseError de) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
System.out.println("loginSuccess = " + loginSuccess);
return loginSuccess;
}
控制台日志
Info: isLoginSuccessful method called
Info: loginSuccess = false
Info: login else
Info: on Child added called
Info: if onChildAdded called
Info: loginSuccess = true
发生了什么事:
processRequest()被调用
isLoginSuccessful()在 loginSuccess为假
3.if processRequest()的语句被调用
我想要的是:
processRequest()被调用
isLoginSuccessful()被调用一次,其中 loginSuccess为真
3.if processRequest()的语句被调用