使用jquery检索数据

时间:2016-03-18 03:18:03

标签: jquery html twitter-bootstrap jsp

如何使用jquery从jsp中检索数据? 我创建了一个基本表单,然后将值存储到mysql数据库中。然后我使用jsp.Now检索数据库值。我希望我的jquery从jsp代码中获取这些值并打印这些值。 这是我的jsp代码

<body style="background-color:#663366">
  <div class="container">
    <br />
    <div class="jumbotron">
      <h1>Authentica</h1>
      <p>Have an account already? Then login</p>
    </div>
    <form class="form-horizontal" action="newjsp.jsp" method="GET">
      <div class="form-group">
        <label class="control-label col-sm-2" for="email" style="color:#FFFFFF">Email:</label>
        <div class="col-sm-3">
          <input type="email" class="form-control" name="vlemail" id="lemail" placeholder="Enter email" />
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <input type="submit" class="btn btn-default" value="Next" />
        </div>
      </div>
    </form>
    </center>
  </div>
</body>

这是我的HTML代码

{{1}}

现在我希望我的jquery从jsp获取数据并将其打印出来

1 个答案:

答案 0 :(得分:0)

  

<强>针对home.jsp

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
  <div class="col-sm-3">
    <input type="text" class="form-control" name="vlemail" id="lemail" placeholder="Enter email" />
  </div>

  <input type="submit" id="aaa" value="Submit" name="Submit">
  <br />
  <div id="javaquery"><b>Name will be displayed here</b></div>
</body>

<script type="text/javascript">
$(document).ready(function() {
  $("#aaa").click(function() {
    var value = $("#lemail").val();

    $.get("data.jsp", {
      q: value
    }, function(data) {
      $("#javaquery").html(data);
    });
  });
});
</script>
  

<强> data.jsp

<body>

  <%
            String name = "";
            String q = request.getParameter("q");
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://192.168.99.146:3306/tables", "root", "Abcdefg1");
                Statement smt = con.createStatement(); //Create Statement to interact
                ResultSet r = smt.executeQuery("select * from employee where(email='" + q + "');");
                while (r.next()) {
                    name = r.getString("name");
                }
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        %>
    Name:
    <%out.print(name);%>
</body>