0

I want to pass in few string to servlet using AQuery, by calling for loop to pass each string to my servlet. Below is my code:

String str = "st1-st2-st3";
    String delimiter = "-";
    String[] temp;
    temp = str.split(delimiter);
    for(int i =0; i < temp.length ; i++){
                System.out.println("temp[i]->"+temp[i]);
                params.put("myValue", temp[i]);

                aq.ajax(url, params, String.class, new AjaxCallback<String>() {
                    @Override
                    public void callback(String url, String html, AjaxStatus status) {
                        //prodStr = html;
                        //showList1(prodStr, sortingAsc);
                        System.out.println("Show return");
                    }
            });
        }

Servlet code

String myValue = request.getParameter("myValue");

System.out.println("Show myValue->"+myValue);

I want my result become:

temp[i]->st1
Show return
temp[i]->st2
Show return
temp[i]->st3
Show return

But my code run the result as:

temp[i]->st1
temp[i]->st2
temp[i]->st3
Show return

I want it to call three times to the servlet, instead of once. In my print out servlet code is always show the last string value st3, st1 and st2 is missing. Is this possible? Please help, thanks.

Arzozeus
  • 103
  • 2
  • 11
  • First thing, You havent use temp[i] in your code except printing and you need to check if any exception is there at server end (or Ajax method implementation), which is stopping printing "Show return" – Ninad Pingale Jul 21 '14 at 06:04
  • Please refer this link. http://stackoverflow.com/questions/13241668/how-to-send-array-to-servlet-using-jquery-ajax – Prabhakaran Ramaswamy Jul 21 '14 at 06:08
  • @ninad sorry for the mistake, I have edit my code. I put temp[i] into params myValue to the servlet. When I run my code, its always show the last value "st3" at myValue, how can I pass each value each time? – Arzozeus Jul 21 '14 at 06:21
  • Try adding a delay in the for loop after execution of each index.. – Ninad Pingale Jul 21 '14 at 06:25
  • @Prabhakaran, I am able to get value in servlet, I have update my code . I am not able to get st1 and st2, the servlet always get the last string in my array, which is st3. How can I pass each string one by one?any idea? thanks – Arzozeus Jul 21 '14 at 06:28
  • 1
    http://www.ravenglass.com/blog/index.cfm/2013/3/5/FOR-Loops-Overwriting-Ajax-Responses-and-how-to-fix – Ninad Pingale Jul 21 '14 at 06:28
  • @NinadPingale Thanks for your help, the link your share is work – Arzozeus Jul 21 '14 at 06:37
  • > Ajax call: make async: false [link for more information](http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax) – Subha Chandra Oct 19 '15 at 13:16

0 Answers0