admin管理员组文章数量:1424963
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?
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?
Share Improve this question asked Jul 11, 2012 at 9:12 DarkSideUserDarkSideUser 5653 gold badges12 silver badges28 bronze badges 5- Why do you want your ordering to be done on server side ? Why can't you do it in Javascript ? – Tusc Commented Jul 11, 2012 at 9:18
- Also, how do you want to start the call? Submit form, click a certain button etc; what will trigger the action? – mhan Commented Jul 11, 2012 at 9: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. – DarkSideUser Commented Jul 11, 2012 at 9:40
- What do you mean by "many" ? 100 ? 1000 ? 10000 ? Modern browsers have optimized javascript engines. If your ordering task doesn't involve plex algorithm, try to implement the obvious and simplest solution (order on client side) and make your own opinion about speed – Tusc Commented Jul 11, 2012 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. – DarkSideUser Commented Jul 11, 2012 at 10:44
1 Answer
Reset to default 4You 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/blogs/creating-parsing-json-data-with-java-servlet-struts-jsp-json/
http://srikanthtechnologies./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;
//Your Java Servlet class
package .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);
}
}
本文标签: javascriptretrieve data from java servlet in a webpageStack Overflow
版权声明:本文标题:javascript - retrieve data from java servlet in a webpage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428617a2658227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论