I have this processRequest(..)
method in a Servlet class, where I am checking user's email and password in isLoginSuccessful(..)
method using ChildEventListener
of Firebase and want to redirect the user to home.jsp if true or back to index.jsp if false. But the boolean loginSuccess
in this isLoginSuccessful(..)
method returns false
, hence redirect back to index.jsp then returns true
. Means isLoginSuccessful(..)
gets interrupted. How to fix this?
processRequest() method :
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() method :
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;
}
Console logs
Info: isLoginSuccessful method called
Info: loginSuccess = false
Info: login else
Info: on Child added called
Info: if onChildAdded called
Info: loginSuccess = true
What is getting happened :
processRequest() gets called
isLoginSuccessful() gets called where loginSuccess is false
3.if statement from processRequest() gets called
- isLoginSuccessful() gets called again where loginSuccess is true now.
What I want is :
processRequest() gets called
isLoginSuccessful() gets called one time where loginSuccess is true
3.if statement from processRequest() gets called