如何在JSP中对输入框的文本更改执行查询

时间:2015-11-20 08:22:49

标签: java jsp text input

现在我正在执行此代码,该代码正在处理提交按钮单击。

<%@page import="java.sql.CallableStatement"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="connectionss.Connectionss"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form  action="ShowUSerInfo.jsp" method="post">                                     
        <input type="text"  name="tagID">
        <input type="submit" value="Search" name="submit" >          
</form>

             <%
             String TAGID="";
             String y=request.getParameter("submit");

                 if("Search".equals(y))
                    {
                TAGID = request.getParameter("tagID");
                out.println(TAGID);


                    String GetUserpass = "{call sp_USerInfoByTagID(?)}";
                    //Connection conn = Connection.GetConnection();
                    CallableStatement cs;
                    cs =  Connectionss.GetConnection().prepareCall(GetUserpass);
                    cs.setString(1,TAGID);
               %>

                    <table border="1">
                    <th>First Name</th>
                    <th>last Name Name</th>
                    <th>Student ID</th>
                    <th>Address</th>
                      <th>NIC</th>

                    <% 
                        ResultSet rs1;
                        rs1=cs.executeQuery();
                        while(rs1.next()){

                            String UserName=rs1.getString("fId_FirstName");

                            String UserID= rs1.getString("fId_User_UniversityID");
                            String Adress = rs1.getString("fId_Address"); 
                            String NIC = rs1.getString("fId_NIC");
                            String lastName = rs1.getString("fId_LastName");

                            %>                    

                <tr >

                <td><span style="color:red;"><%= UserName %></span></td>
                <td> <span style="color:red;"><%= lastName %></span></td>
                <td><span style="color:red;"><%= UserID %></span></td>
                <td><span style="color:red;"><%= Adress %></span></td>
                <td><span style="color:red;"><%= NIC %></span></td>

                </tr>

                </table>

           <%}} %>                    

</body>
</html>

但我想在输入框中更改文本时执行此代码。基本上文本框从NFC卡获得价值。所以,当我在阅读器上点击我的NFC卡时,值是传输到文本框,此时我想执行我的SQL查询,该查询将使用户信息对抗TAGID。

2 个答案:

答案 0 :(得分:1)

因为jsp是服务器端,所以你需要在客户端收集输入并进行ajax调用。

<form  action="ShowUSerInfo.jsp" method="post">
       <input type="text" id="text"  name="tagID">
       <input type="submit" value="Search" name="submit" >
</form>
    <div id="#res></div>

javascript:

   $('#text').change(function(e){
      $.ajax({url: "someurl", success: function(result){
             $("#res").html(result);
          }});
        });
  });

答案 1 :(得分:0)

这将完全适用于您的情况,

让我们说我们的HTML看起来像,

<form>
    <input id="txt" type="text" name="name"/>
    <input id="btn" type="button" value="change"/>
</form>

Java-Script like,

$(document).ready(function(){
        $('#txt').keypress(function(e){
            ontextchange();
          });
        $('#txt').keyup(function(e){
            if(e.keyCode == 8)
               {
                  //if user erase from text-box then comes here...
               }
           });

        function ontextchange(){
        //do whatever while text change, i.e. ajax call or something else...
        }
    });