I have written following two jsp files. First file i.e. submit.jsp has form where user submits her query and her city and latitude and longitude are recorded and she is forwarded to another jsp page which also has a form where she can again submit her query as many times as she wants and I want the values of city , latitude and longitude to be retained but they are getting reset. The code of the two files are as follows:
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>
I am new to JSP. So can anyone please help me to fix the given code.