为什么servlet类中具有firebase的childeventlistener的此自定义java方法从完成时就被注入?

时间:2019-01-30 14:07:33

标签: java firebase servlets firebase-realtime-database

我在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 

发生了什么事:

  1. processRequest()被调用

  2. isLoginSuccessful()在 loginSuccess为假

  3. 时被调用

3.if processRequest()的语句被调用

    现在,如果loginSuccess为true,则再次调用
  1. isLoginSuccessful()。

我想要的是:

  1. processRequest()被调用

  2. isLoginSuccessful()被调用一次,其中 loginSuccess为真

3.if processRequest()的语句被调用

0 个答案:

没有答案