1

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%">&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>

I am new to JSP. So can anyone please help me to fix the given code.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Joy
  • 4,197
  • 14
  • 61
  • 131

3 Answers3

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>
Mehul Kaklotar
  • 365
  • 1
  • 19
0

try to get the parameters at the beginning of the file.

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
Mehul Kaklotar
  • 365
  • 1
  • 19
  • No Sir, it's not working. – Joy Mar 29 '13 at 06:04
  • In your code, in submit.jsp you are getting the values of latitude,longitude an location. and that values you are submiting when form is submitted.so first time when form is submitted, it will get the values. but in the process.jsp page when you click on the submit button,it will not have that latitude n longitude values(your javascript code that gives you location which is on submit.jsp) – Mehul Kaklotar Mar 29 '13 at 06:38
  • That is why I am asking will there be any way to retain the value of those variables even if the form is submitted multiple times. – Joy Mar 29 '13 at 07:28
  • can be done using only 1 page submission redirecting to same page. – Mehul Kaklotar Mar 29 '13 at 07:54
  • But Sir if I redirect to another page like the above I have done is there no way to do that?? Because I need submission of forms redirect to another page. – Joy Mar 29 '13 at 11:45
0

like this ...

<%@ 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>
Mehul Kaklotar
  • 365
  • 1
  • 19