1

I have a webpage which shows some info. In my webpage I have many coordenates (lon, lat) and I want to make a java class to ordenate these coords.

Which I am trying to do is before showing coords, send them to the java servlet. This servlet would ordenate them and send back to the webpage to show them.

I want that webpage doesn't refresh so I think I need ajax to call servlet, I have it more or less under control. But I don't know how to receive data in java from a webpage.

I created some servlets in past but always calling java function using javascript. The problem of doing it by this way is that web browser goes to other webpage (jsp with java calls).

I don't have yet the code for ordering coords but I am using this to try:

package ordenacion;

public class OrdenarListaPuntos {


public static String ordenar(String cadenaDesordenada){  //String unordered
    String cadenaOrdenada;

//here goes the code for ordering
    cadenaOrdenada = cadenaDesordenada;
    return cadenaOrdenada; //String ordered
}
}

I just know how to call java function from javascript using jsp but not from other webpages in other servers or without refreshing webpage...

Do you have any idea about how I should do it? Or what I should read about it?

Biribu
  • 535
  • 3
  • 12
  • 27
  • Why do you want your ordering to be done on server side ? Why can't you do it in Javascript ? – Tusc Jul 11 '12 at 09:18
  • Also, how do you want to start the call? Submit form, click a certain button etc; what will trigger the action? – mhan Jul 11 '12 at 09:24
  • I want to order them in server side because maybe there are many coordenates and I think is possible that if I do on client side it would be slowly. For starting the call I have a javascript function which is called during load page. That function get the coordenates list and should send to the server for ordering. – Biribu Jul 11 '12 at 09:40
  • What do you mean by "many" ? 100 ? 1000 ? 10000 ? Modern browsers have optimized javascript engines. If your ordering task doesn't involve complex algorithm, try to implement the obvious and simplest solution (order on client side) and make your own opinion about speed – Tusc Jul 11 '12 at 10:15
  • I don't know the exactly number but I think more or less 200-300. My order algorithm will be similar to djistra with some differences but similar. The problem is that I have also route table with distances in the server side. So webpage should ask each time it needs a distance to server and that would be a huge amount of traffic. – Biribu Jul 11 '12 at 10:44

1 Answers1

4

You need to do a bit of reading, as you are trying to grasp more than just a simple concept. Take a look at these for a start;

How do you send an array as part of an (jquery) ajax request

http://codesstore.blogspot.co.uk/2011/12/json-with-jquery-jsp-servlets.html

http://viralpatel.net/blogs/creating-parsing-json-data-with-java-servlet-struts-jsp-json/

http://srikanthtechnologies.com/blog/java/jobs_employees_jquery.html

In the end you are going to use this basic structure. You are going to use a POST instead of the GET that I mentioned in here

var valSend = "aSingleParam";

var url = "/yourApplicationWebContext?paramPassed="+valSend;
console.log(url);
$.ajax({
    url: url,
    type: "GET",
    dataType: "json",
    success: function(data) {
    console.log("Data returned : " + data);



        if (typeof data == 'object') {

                }

        },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("jqXHR : "+jqXHR + " text status : " + textStatus + " error : " + errorThrown);
        }
    });

Java Servlet Side.......

Your web.xml will have a servlet and corresponding servlet mapping;

servlet mapping

//Your Java Servlet class

package com.xyz;

public class ServlvetClassName extends HttpServlet {

//The type: "GET" in the ajax call will trigget a "get" which the doGet will handle

protected void doGet(HttpServletRequest req, HttpServletResponse response)
        throws ServletException, IOException {

        if(null!= req.getParameter("paramPassed")){

         // get and use your parameter, which "is paramPassed"..... 

        }


        }

//The type: "POST" in the ajax call will trigget a "post" which the doPost will handle

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    this.doGet(req, resp);
}

}

Community
  • 1
  • 1
mhan
  • 373
  • 3
  • 11
  • Thanks. I think what I thought for ajax call is similar to your code. Where I have the problem is in java side. I don't know how to call method I want and how to send data to that method... I will check your pages, thanks again. – Biribu Jul 11 '12 at 11:13