1

我写了以下两个jsp文件。第一个文件,即 submit.jsp 有表单,用户提交她的查询,她的城市和纬度和经度被记录下来,她被转发到另一个 jsp 页面,该页面也有一个表单,她可以再次提交她的查询,只要她愿意,我希望保留 city 、 latitude 和 longitude 的值,但它们正在重置。两个文件的代码如下:

提交.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<head>
 <script type="text/javascript">
  function getLocation(){
  if (navigator.geolocation){
      navigator.geolocation.getCurrentPosition(showPosition);
    }
  }
  function displayLocation(latitude,longitude){
    var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
      if(request.readyState == 4 && request.status == 200){
        var data = JSON.parse(request.responseText);
        var address = data.results[0];
        //document.write(address.formatted_address);
        var city=document.getElementById("city");
        var n = address.formatted_address.split(",");
        city.value = n[n.length-3];
      }
    };
    request.send();
  };

function showPosition(position){
//var x = document.getElementById("demo");
var latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
//x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
displayLocation(latitude.value,longitude.value);
}

</script>

</head>
<body onload="getLocation()">
<form name="frm" method="post" action="process.jsp">
<input type="text" name="myQuery" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input name="city" id="city" type="hidden">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

进程.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%! String city; String latitude; String longitude; %>
<form name="frm" method="post" action="process.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">&nbsp;</td>
<td width="78%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp; </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td>&nbsp;</td>
<input name="latitude" id="latitude" type="hidden" value="${latitude}" >
<input name="longitude" id="longitude" type="hidden" value="${longitude}" >
<input name="city" id="city" type="hidden" value="${city}"  >
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
<html>
<body>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>

我是 JSP 的新手。所以任何人都可以帮我修复给定的代码。

4

3 回答 3

1
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%! String city; String latitude; String longitude;String query; %>


<%
query=request.getParameter("myQuery");
city=request.getParameter("city");
latitude=request.getParameter("latitude");
longitude=request.getParameter("longitude");
%>
<html>
<body>
<form name="frm" method="post" action="process.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">&nbsp;</td>
<td width="78%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp; </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td>&nbsp;</td>
<input name="latitude" id="latitude" type="hidden" value="<%=latitude%>" >
<input name="longitude" id="longitude" type="hidden" value="<%=longitude%>>" >
<input name="city" id="city" type="hidden" value="<%=city%>"  >
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>
于 2013-03-29T12:16:28.793 回答
0

尝试获取文件开头的参数。

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
于 2013-03-29T05:35:26.610 回答
0

像这样 ...

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
    <html>
    <head>
     <script type="text/javascript">
      function getLocation(){
      if (navigator.geolocation){
          navigator.geolocation.getCurrentPosition(showPosition);
        }
      }
      function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
      if(request.readyState == 4 && request.status == 200){
        var data = JSON.parse(request.responseText);
        var address = data.results[0];
        //document.write(address.formatted_address);
        var city=document.getElementById("city");
        var n = address.formatted_address.split(",");
        city.value = n[n.length-3];
      }
    };
    request.send();
  };

function showPosition(position){
//var x = document.getElementById("demo");
var latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
//x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
displayLocation(latitude.value,longitude.value);
}

</script>

</head>
<body onload="getLocation()">
<%! String city; String latitude; String longitude; %>
<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
<form name="frm" method="post" action="submit.jsp">
<input type="text" name="myQuery" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input name="city" id="city" type="hidden">
<input type="submit" name="submit" value="Submit">
</form>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>
于 2013-03-29T07:56:31.677 回答