如何使用Ajax连接数据库(jsp)检查输入验证

How to check input validation using Ajax connect to database (jsp)

本文关键字:检查 输入 验证 jsp 何使用 Ajax 连接 数据库      更新时间:2023-09-26

代码用于检查用户输入的大学是否已经存在于数据库中。如果是,那么提交输入并进入下一页;如果没有,则向用户发送一条警告消息,并停留在相同的页面上,该页面为choose_university.jsp。jsp用于连接到数据库并执行检查。但是代码没有这样做。我花了好几个小时研究它,但还是没弄明白。谁能告诉我有什么问题,并告诉我如何解决它?明天就要交了。请帮帮我。

choose_university.jsp如下:

<%@page import="java.util.*"%>
 <html>
 <head><title>Provide degrees - choose university</title> 
 <script type="text/javascript">
 function validate() {
     var xmlHttp;
     xmlHttp = new XMLHttpRequest();
     if (xmlHttp == null) {
     alert("Your browser does not support AJAX!");
     return;
      }
     var u = document.getElementById("university").value;
     var url = "checkUniversity.jsp";
     url = url + "?university=" + u;
     xmlHttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4 ) {
          document.getElementById("university").innerHTML = xmlhttp.responseText;
        }
     }
     alert("yea we got 55555");
     xmlHttp.open("GET", url, true);
     xmlHttp.send(null);
  }

  function GetXmlHttpObject() {
      var xmlHttp = null;
      try {
          // Firefox, Opera 8.0+, Safari
           xmlHttp = new XMLHttpRequest();
      } catch (e) {
            // Internet Explorer
     try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
     }
     return xmlHtp;
  }
   </script>
   </head>
    <body>
   <br> If you can't find your university, please provide it in the following and hit submit <br>
   <form method="post" action="Provide_degrees_Choose_discipline.jsp" onsubmit = "return validate()">
    <p>To manually add your university </p> <br>
    <p> name of university: <input type = "text" id="university"  name = "university" />        </p><br>
    <input type="submit" name = "submit" value="submit"  />
    </form>
     </body>
     </html>

       /*  checkUniversity.jsp  */
      <% response.setContentType("text/xml") ; %>
     <%@ page import="javax.sql.*"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
      <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
      <%@ page import="model.ApplicationModel" %>
    <html>
     <head><title>check university</title> 
    </head>
     <body>
     <%
          System.out.println("heyheyhey");
           String u = request.getParameter("university") ;
    Class.forName("org.postgresql.Driver");
        // Open a connection to the database using DriverManager
        conn = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/access?" +
                "user=postgres&password=neshorange");
        // Create the statement
        Statement statement = conn.createStatement();
            // Use the created statement to SELECT
            // the student attributes FROM the Student table.
        rs = statement.executeQuery("SELECT count(*) as c FROM universities WHERE university=''"+ u +"'';");
      if (rs.next()){
       if ( rs.getInt("c") > 0) {
           response.write("false");
       } else {
       response.write("true");
       }
      }
       response.write("true");
        %>
    </body>
    </html>

试着去掉onSubmit中的"return",然后再试一次。同时安装firebug或其他检查器(如果你还没有),这样你就可以看到javascript和请求错误。

现在也没有必要像这样遍历所有ajax的东西。看看jQuery或Mootools之类的javascript库。

试试这个

function validate()
{
    var u = document.getElementById("university").value;
    $.post('checkUniversity.jsp?university=' + u, function(data) {
      if(data==true) return true;
      else
          {
              alert("user doesnot exists ")
              return false;
          }
});
}

如果用户从checkUniversity.jsp

返回true