0

Following code, I am using to get GeoLocation in jsp/javascript/html.

<script>
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        alert(position.coords.latitude + ", " + position.coords.longitude);
        });

    }

</script>

Now, I need to store this values in database, which query is in jsp tag. How can we store this latitude, longitude values with a variable and then pass to query.

  • this may help you http://stackoverflow.com/questions/3028490/calling-a-java-servlet-from-javascript after this servlet call method to store in db – Taha Jun 18 '16 at 15:07

1 Answers1

1

Option 1:-

If you are going to submit any form in the jsp the put a hidden value like below

<input type="hidden" id="geoLocation" >

Then set this when page starts loading i.e. in windows.onLoad or after loading the hole page i.e. in jQuery $document.ready{}

Set value as

if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function (position) {
      document.getElementById("geoLocation").value=position.coords.latitude + ", " + position.coords.longitude;
        }
      );
}

And then submit it with the form...

Option 2 :-

Try to use AJAX DWR component which will let you add data to the data base with a single DAO class i.e. simple POJO class

All steps to do it are listed on its official website :- http://directwebremoting.org/dwr/introduction/getting-started.html

You will need to include 2 javascript file to your jsp and give DWR access to your DAO methods if you are using MVC but in this case i prefer to make seprate DAO just for DWR and you are Done...

Shalin Patel
  • 1,079
  • 1
  • 10
  • 15