从jsp加载servlet并尝试传递多个参数

时间:2014-10-30 06:17:08

标签: javascript html jsp xmlhttprequest

我试图使用jsp中的java脚本加载servlet。我也试图传递两个参数。页面加载,但现在我从servlet打印出一个sytem,它假设给我id和车辆vaules,但它给车辆felid为null。

ScanServlet doGet被称为id = 1 vehicle = [object HTMLParagraphElement] vehicle = null

这是代码

  <body onload="notifyOnLoad(<%=request.getParameter("imgid")%>)">
    <img id="imgBarcode" src="ImageServlet?imgid=<%=request.getParameter("imgid")%>&vehicle=<%=request.getParameter("vehicle")%>" width=<%=request.getAttribute("Width")%> height=<%=request.getAttribute("Height")%>><br/>
    <h4>Barcode Details</h4>
    <p>Name :- <%=request.getAttribute("name")%></p>
    <p>Type :- <%=request.getAttribute("type")%></p>
    <p>Value :- <%=request.getAttribute("value")%></p>
    <p id="wtf">type :- <%=request.getParameter("vehicle")%></p>
    <script type="text/javascript">
        function notifyOnLoad(imgid) {
            var xmlhttp;
            var vehicle =document.getElementById("wtf");
            if(window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari;
                xmlhttp = new XMLHttpRequest();
            }
            else{
                /*code for IE6, IE5*/
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    window.location = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "ScanServlet?imgid="+imgid+"vehicle="+vehicle, true);
            xmlhttp.send();
        }
    </script

>
 </body>

1 个答案:

答案 0 :(得分:0)

您无法以您尝试的方式直接访问javascript中的请求参数。

<input type='hidden' name='field1' id='imgid' value='<%=request.getParameter("imgid")%>' />

使用其ID

访问javascript中的变量
<script type="text/javascript">
        function notifyOnLoad(imgid,vehicle) {
            var xmlhttp;
            var imgid=document.getElementById("imgid");
            xmlhttp.open("GET", "ScanServlet?imgid="+mgid+"&vehicle="+vehId, true);
            xmlhttp.send();
        }