我写了两个jsp文件。第一个文件即submit.jsp有一个表单,用户提交她的查询,她的城市和纬度和经度被记录下来,她被转发到另一个jsp页面,该页面也有一个表格,她可以再次提交她想要的次数和我希望保留城市,经度和经度的值,但它们会被重置。这两个文件的代码如下:
submit.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>
process.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%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td> </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td> </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> </td>
<td> </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的新手。所以任何人都可以帮我修复给定的代码。
答案 0 :(得分: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%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td> </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td> </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> </td>
<td> </td>
</tr>
</table>
</form>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>
答案 1 :(得分:0)
尝试在文件的开头获取参数。
<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
答案 2 :(得分: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>