2016-03-18 88 views
0

如何使用jquery從jsp檢索數據? 我創建了一個基本表單,並將值存儲到mysql數據庫中。然後使用jsp.Now檢索數據庫值,我希望我的jquery從jsp代碼獲取這些值並打印這些值。 這是我的jsp使用jquery檢索數據

<% 
    String vlemail=request.getParameter("vlemail"); 
    try 
    { 
    String str="jdbc:mysql://localhost:3306/licet"; 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    Connection con=null; 
    con=DriverManager.getConnection(str,"root",""); 
    Statement st=con.createStatement(); 
    ResultSet rs =st.executeQuery("select Password from record where Email='"+vlemail+"'"); 
    while(rs.next()) 
    { 
    String a=rs.getString("Password"); 
    } 
    out.println("Retrieved"); 
    } 
    catch(Exception e) 
    { 
    out.println(e); 
    out.println("Not retrieved"); 
    } 
    %> 

代碼,這是我的html代碼

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

現在,我想我的jQuery從JSP獲取數據並打印

回答

0

家。 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>