Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将javascript中设置的参数从一个jsp发送到另一个jsp_Javascript_Jsp - Fatal编程技术网

将javascript中设置的参数从一个jsp发送到另一个jsp

将javascript中设置的参数从一个jsp发送到另一个jsp,javascript,jsp,Javascript,Jsp,我对JSP和javascript非常陌生。我正在使用javascript计算两个地方之间的距离,并将距离存储在文本框中。document.getElementById'dist'。值=行驶距离公里 但是,当我尝试使用stringdist=request.getParameterdist检索值时;我没有价值 我还尝试使用document.location.href=bookings.jsp?dist=+drivingDistanceKM; 这是我第一次这么做,但当我再次尝试时,它没有任何价值 我如

我对JSP和javascript非常陌生。我正在使用javascript计算两个地方之间的距离,并将距离存储在文本框中。document.getElementById'dist'。值=行驶距离公里

但是,当我尝试使用stringdist=request.getParameterdist检索值时;我没有价值

我还尝试使用document.location.href=bookings.jsp?dist=+drivingDistanceKM; 这是我第一次这么做,但当我再次尝试时,它没有任何价值

我如何解决这个问题

共享更多代码:

这是在book.jsp中

Javascript:

<script>
var geocoder, location1, location2, gDir;

function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();        
}

function showLocation() {
    geocoder.getLocations(document.form2.pickUp.value, function (response) {
        if (!response || response.Status.code != 200)
        {
            alert("Sorry, we were unable to geocode the first address");
        }
        else
        {
            location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
            geocoder.getLocations(document.form2.dropOff.value, function (response) {
                if (!response || response.Status.code != 200)
                {
                    alert("Sorry, we were unable to geocode the second address");
                }
                else
                {
                    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                    gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                    calculateDistance();
                }
            });
        }
    });
}

function calculateDistance()
{
    GEvent.addListener(gDir, "load", function() {

        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
        document.getElementById('dist').value = drivingDistanceKilometers;

    });
}
</script>
HTML:

Inside bookings.jsp:

<body>
<% 
String dist = request.getParameter("dist"); 
out.println(dist);
%>
</body>

实际上,您没有名为dist的输入字段。您需要做的是修改html并将其插入表单中

因此,您的代码可能如下所示:

<form name="form2" onSubmit=" showLocation(); " method="post" action="bookings.jsp" >

<input type="hidden" id="dist"  value="0"/>
<input name="pickUp" type="text">
<input name="dropOff" type="text">
<input type="submit" value="Submit">

</form>

您确定您在中有价值吗drivingDistanceKilometers@Tariq对如果我没有表单操作,那么当我点击我的提交按钮时,距离值就会出现在文本框中。实际上是使用普通JSP。与HTML或PHP页面相比,没有任何区别。你需要仔细检查你的代码。或者最好多分享一些代码,让我们更好地了解problem@Tariq共享了更多代码。您调试了Javascript吗?能否在行文档上设置断点。getElementById'dist'。value=drivingDistanceKymbers;?我试过了,但没用。。所以我把字体改成了文本。现在它在文本框中显示距离,但在out.printlndist;给我的值为0
<form name="form2" onSubmit=" showLocation(); " method="post" action="bookings.jsp" >

<input type="hidden" id="dist"  value="0"/>
<input name="pickUp" type="text">
<input name="dropOff" type="text">
<input type="submit" value="Submit">

</form>