为什么就绪状态永远不会达到4?

问题描述 投票:0回答:1

我是Java Servlet和Ajax的新手。我想验证用户是否输入了正确的密码,但是ajax中的就绪状态更改永远不会达到4。它还必须检查电子邮件/用户名是否正确或错误,但代码永远不会达到onreadystatechange()功能表示它永远不会变成4.请帮助。

这里是HTML代码:

<!DOCTYPE HTML>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Notes Finder</title>

  <script type="text/javascript">
  function verify()
   {
      var email=document.forms["Login"]["Email"].value;
      var password=document.forms["Login"]["Password"].value;

    if(email.length==0)
    {
     alert("Email is not submitted.Please check that you have entered the Email");
     return false;
    }
    if(password.length==0) {
      alert("Password is not submitted.Please check that you have entered the Password");
      return false;
    }

    check_email();
  }
  </script>
</head>
<body>
<form name="Login" method="post" onsubmit="return verify()" id="Login" >
<p>Email:</p><input type="text" name="Email" placeholder="UserName"/><p id="emailpara" 
style="color:red;"></p><br><br>
 <p>Password:</p><input type="password" name="Password"  placeholder="Password"/><p 
 id="passwordpara" style="color:white"></p><br><br>
  <input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
function check_email()
 {

    var make_ajax_request=new XMLHttpRequest();
    var original_form=document.getElementById("Login");
    var form=new FormData(original_form);
       make_ajax_request.onreadystatechange=function()
       {

        if(make_ajax_request.readystate==4&&make_ajax_request.status==200)
           {
           alert("Inside check_email");
           var returntext=this.responsetext();
           returntext.toLowerCase();
           document.getElementbyId("emailpara").innerHTML=returntext;
        if(returntext.localeCompare("1"))
            {
            document.getElementbyId("emailpara").innerHTML="";
            document.getElementbyId("passwordpara").innerHTML="";
            }
        else if(returntext.localeCompare("2"))
            {
            document.getElementbyId("emailpara").innerHTML="Email not found.Please verify again";
            }
        else if(returntext.localeCompare("3"))
            {
            document.getElementbyId("passwordpara").innerHTML="Incorrect Password.Please check that 
         you have entered the correct password";
            }

            }
 };
 make_ajax_request.open("post","login",true);
 make_ajax_request.send(form);

 }
</script>
</body>
</html>

这是servlet代码:

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
  ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer=response.getWriter();
     System.out.println("Request received12");
     email=request.getParameter("Email");
     password=request.getParameter("Password");

    System.out.println("Email:"+email+"\nPassword:"+password);
    try {
       Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection(url, user, pass);
    Statement statement=con.createStatement();

    rs=statement.executeQuery("Select email,password_ from "+table_name+" where email='"+email+"'");
    meta=rs.getMetaData();
    if(!rs.next())
    {
      writer.write("2");

    }
    else
    {
        //This is the method used for hashing the password for comparison of password
        MessageDigest sha512=MessageDigest.getInstance("SHA-512");
        sha512.update(salt.getBytes(StandardCharsets.UTF_8));
        byte[] messagedigest=sha512.digest(password.getBytes(StandardCharsets.UTF_8));
        StringBuilder temp=new StringBuilder();
        for(int i=0;i<messagedigest.length;i++)
        {
             temp.append(Integer.toString((messagedigest[i] & 0xff) + 0x100, 16).substring(1));
        }
       hashedpassword=temp.toString();
       System.out.println("Hashed Password="+hashedpassword);

        //Password has been hashed successfully
       if(hashedpassword.equals(rs.getString("password_")))
       {
           Cookie cookie=new Cookie(email,password);
            response.addCookie(cookie);
           System.out.println("Password has been matched");
           writer.write("1");
           response.sendRedirect("");

       }
       else
       {  
         writer.write("3");
        System.out.println("Password has been unmatched");

        }
     }
    writer.close();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    throw new IllegalStateException(e);
} catch (SQLException e) {
    // TODO Auto-generated catch block
    throw new IllegalStateException(e);
} catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
            throw new IllegalStateException(e);
}

}

以上代码返回分配了特定代码以检查错误的数字。

javascript java ajax servlets
1个回答
0
投票

尝试以下操作

© www.soinside.com 2019 - 2024. All rights reserved.