从HTML表单到脚本的Servlet调用

时间:2017-02-20 05:11:52

标签: javascript jquery html servlets

我正在尝试从用户端接收收据编号的简单程序,并在用户点击按钮后发送到servlet。通过脚本调用servlet。

我可以调用servlet,但无法使用req.getparameter在servlet端获取用户输入。我收到空值。请帮助我解决我所缺少的问题。



init(snapshot: FIRDataSnapshot){
    guard let values = snapshot.value as! [String: Any] else {
        return
    }

    self.description = values["description"] as! String
    self.postID = values["postID"] as! String
    self.title = values["title"] as! String
    self.rating = values["rating"] as! String
    self.price = values["price"] as! String
    self.image = values["image"] as! String
    self.key = snapshot.key
    self.ref = snapshot.ref
}






<!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=UTF-8">
    <title>Insert title here</title>
    <script>
        function showCustomer() {
          var xmlhttp;
          var value1 = document.getElementById("num").value; 
  
          xmlhttp = new XMLHttpRequest();
          xmlhttp.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 200) {
                 document.getElementById("demo").innerHTML = this.responseText;
              }
          };
          //xmlhttp.open("GET", "callserv1?q="+str, true);
          //xmlhttp.send();
          xmlhttp.open("GET", "callserv1", true);
          xmlhttp.send();
       }
    </script>
</head>
<body>
    <form action=""> 
        Receipt no
        <input type="text" name="num" id="num"/>
        <input type="button" onclick="showCustomer()" value ="call"/>
    </form>
    <p id="demo">Customer info will be listed here...</p>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

根据您的showCustomer()方法,您需要传递您的价值。看起来你已经做了一些尝试来获取价值,并在你的注释部分传递它,但有两件事:

  1. 您没有传递正确的参数,例如value1
  2. 您已将参数命名为不同的名称,应将其命名为num
  3. 基于此,此代码应该更适合您。

    function showCustomer() {
        var xmlhttp;
        var value1 = document.getElementById("num").value; 
    
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("demo").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "callserv1?num="+value1, true);
        xmlhttp.send();
    }
    
相关问题