weixin_33709609 2014-04-04 08:36 采纳率: 0%
浏览 40

使用Ajax连接数据库吗?

I want to use Ajax for UserId validation, Can anyone help me out in connecting database?

Here is my JSP page . enter code here

   <script type="text/javascript">

    /* 
   * creates a new XMLHttpRequest object which is the backbone of AJAX, 
  * or returns false if the browser doesn't support it 
  */
   function getXMLHttpRequest() { 
   var xmlHttpReq = false; 
   // to create XMLHttpRequest object in non-Microsoft browsers 
    if (window.XMLHttpRequest) { 
      xmlHttpReq = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { 
     try { 
      // to create XMLHttpRequest object in later versions 
      // of Internet Explorer 
      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (exp1) { 
       try { 
        // to create XMLHttpRequest object in older versions 
        // of Internet Explorer 
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (exp2) { 
         xmlHttpReq = false; 
       } 
    } 
   } 
   return xmlHttpReq; 
   } 
  /* 
  * AJAX call starts with this function 
   */
  function makeRequest() 
  { 

 var c=document.getElementById("userid").value;
     var xmlHttpRequest = getXMLHttpRequest(); 
    xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest); 
   xmlHttpRequest.open("POST", "../userid", true); 
   xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-   urlencoded");   
   xmlHttpRequest.send("requestType=ajax&userid="+c); 
    } 

   /* 
   * Returns a function that waits for the state change in XMLHttpRequest 
   */
 function getReadyStateHandler(xmlHttpRequest) { 

   // an anonymous function returned 
   // it listens to the XMLHttpRequest instance 
   return function() { 
    if (xmlHttpRequest.readyState == 4) { 
     if (xmlHttpRequest.status == 200) { 
     document.getElementById("print").innerHTML = xmlHttpRequest.responseText; 
     } else { 
     alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText); 
     } 
    } 
  }; 
 }


    <form  action="<%=application.getContextPath() %>/Login"  method="post"      name="myForm">

  <table>
   <tr>
  <td>UserId</td>
  <td><input type="text" name="userid" id="userid" onblur="makeRequest()" > </td>
   </tr>

   <tr>
   <td>Password</td>
   <td><input type="password" name="password" > </td>
   </tr>

   <tr><td></td>
    <td><input type="submit" name="submit" value="Submit"></td>
   <td><input type="hidden" name="requestType" value="Login"> </td>
  </tr>

  </table>
 </form>
   </script>

Please help me out for this. I require user id validation. If correct userid then it should display name, else display error msg.

  • 写回答

2条回答 默认 最新

  • weixin_33711641 2014-04-04 08:45
    关注

    make a jsp page with database connectivity that will be requested for output.....

    in your ajax request send user_id and in jsp page get userid and check it from database ...if available then send true to ajax otherwise false.....

    or in ajax response get message result from jsp page...make condition to handle this........

    评论

报告相同问题?