java - 如何在不使用表单的情况下将文本字段数据从 html 传递到 servlet

标签 java javascript ajax jsp jakarta-ee

我正在制作一个在线购物网站。我想为用户提供一个选项,他/她可以选择不同的送货地址:-

运送详情

         <input type="button" value="Same as billing address"   style="color: #FFFFFF;" class="link-style" onclick="test();"  />
             <table cellpadding="20">
                    <tr>
                        <td >Name : * </td>
                        <td><input type="text" id="name" name="name" /></td>

                    </tr>
                     <tr>
                        <td>Contact Number : </td>
                        <td><input type="text" name="cno" id="cno"  /></td>
                    </tr>
                    <tr>
                        <td>Address : * </td>
                        <td><input type="text" name="address "id="address" /></td>
                    </tr>

                    <tr>
                        <td>City : *</td>
                        <td><input type="text" name="city" id="city"  /></td>
                    </tr>
                    <tr>
                        <td>State : * </td>
                        <td><input type="text" name="state" id="state" /></td>
                    </tr>
                    <tr>
                        <td>Country : *</td>
                        <td><input type="text" name="country" id="country" /></td>
                    </tr>


                </table>                

如果用户单击按钮,则所有字段都将被禁用。否则他/她可以填写不同的送货地址。问题是我不想在这里使用表单。如何在session或post中将这些参数传递给jsp或servlet?我已经尝试过 AJAX,但无法正确使用它

function setValue() {
    alert("hi");
    var name = document.getElementById("name").value;
    var cno = document.getElementById("cno").value;
    var address = document.getElementById("address").value;
    var city = document.getElementById("city").value;
    var state = document.getElementById("state").value;
    var country = document.getElementById("country").value;
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("post", "test.jsp", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-      urlencoded");
    xmlhttp.send("name=" + name);
    xmlhttp.send("cno=" + cno);
    xmlhttp.send("address=" + address);
    xmlhttp.send("city=" + city);
    xmlhttp.send("state=" + state);
    xmlhttp.send("country=" + country);



}

Test.jsp 未获取参数。

最佳答案

您只能调用 xmlhttp.send 一次,这需要您连接请求参数。

 xmlhttp.send("name="+name+"&cno="+cno+"&address="+address+"&city="+city+"&state=" + state + "&country="+country);

关于java - 如何在不使用表单的情况下将文本字段数据从 html 传递到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22422160/

相关文章:

java - 为什么在尝试对 ResultSet 中检索到的列的值求和时出现错误?

java - 将 Ant 与 PHPUnderControl 一起使用会出现 java.lang.NoClassDefFoundError

java - 学习 Apache 兴趣点

php - 如何从远程服务器收集 HTML 源响应?

javascript - Vue - 无法将 HTML 地理位置绑定(bind)到组件变量

javascript - jquery追加后运行函数

ajax - Grails formRemote:为什么总是重定向?

java - 如何设置jetty runnier应用程序的WebAppContext?

javascript - google script (JS) - 最大递归深度

javascript - Jquery:如何在ajax中添加重复数据