1

I have a JS that I send coordinates from that into a Servlet.

In my JS I get coordinates from a jsp page like this:

 function main1() {
  $.ajax({
    url: 'ServeltConnection',
    type: "GET",
    dataType: "json",
    data: {
       latitude: pos.lat,
       longitude: pos.lng,

     },
    success: function(data){

    //call of  functions
    }
});
}     

I need these coordinates so as to send them into my servlet for getting a return into a json format.

In my Servlet:

 public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
        response.setContentType("application/json");
        PrintWriter out = new PrintWriter(response.getWriter(), true);
    List<Shop> shops;

    try{

    //code for connection with database
            shops=getShops(request.getParameter("latitude"),request.getParameter("longitude"));

        String json = new Gson().toJson(shops);
        response.getWriter().write(shops);
        db.close();

} catch (Exception ex) {
  out.println("Exception: " + ex.getMessage());

When I give specific coordinates in latitude and longitude I GET my json right, but when I' m trying to get coordinates from the js it's not working. I think that the wrong is the way I parse longitude and latitude from js..I searched for parsing JSON but I can't do it. Can you help me?

3
  • Do you mean "pass" your data to the servlet Commented Jan 7, 2016 at 13:35
  • yes that i meant, sorry!! Commented Jan 7, 2016 at 13:36
  • I want to take the coordinates in which I am right now, and put markings on the map in the nearest shops. But it seems that servlet doesnt get the coordinates in shops=getShops(request.getParameter("latitude"),request.getParameter("longitude")); Commented Jan 7, 2016 at 13:40

1 Answer 1

1

You must put parameters of GET requests on the url, like this way:

url: 'ServeltConnection?latitude="+pos.lat+"&longitude="+pos.lng+"'

As well don't forget to remove the 'data' parameter

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.