0

I have a list box in an HTML form. I want to send the list items to a servlet using jquery and AJAX. At the server side, I want to add the sent list items one by one in a database. I tried this using an array in my jsp where I filled it with list items,and then sent it using AJAX, but at servlet I am getting NULL.

my jsp code:

var arr=new Array();
    var rel=$("#rcombo :selected").text();
    $("#listbox  option").each(function(){
        arr[0]=$("#listbox").text();
    });     

    $.ajax({
        type:"get",
        url:"NewServlet",
        data:{list:arr,rel:rel},
      success:function(){}
});

servlet code:

String list[]=request.getParameterValues("list");
System.out.println(list);

Then I changed the code a little bit:

jsp:

var arr=new Array();
    var rel=$("#rcombo :selected").text();
    $("#listbox  option").each(function(){
        arr[0]=$("#listbox").text();
    });     

    $.ajax({
        type:"get",
        url:"NewServlet",
        data:{list:arr[0],rel:rel},
      success:function(){}
});

servlet:

String list[]=request.getParameterValues("list");
System.out.println(list[0]);

Now, I am getting the data. But, all items are in list[0]. I am not able to extract them one by one. Please help me.

0

1 Answer 1

0

Try this

$("#listbox  option").each(function(){
        arr.push($(this).text()); // this line push all text in array
});

Change

 data:{list:arr,rel:rel},

AJAX

$.ajax({
            url:"NewServlet",
            type:"POST",
            dataType:'json',
            data: {list:arr,rel:rel},
            success:function(data){
                // codes....
            }
     });

Servlet

String[] list = request.getParameterValues("list[]");
Sign up to request clarification or add additional context in comments.

11 Comments

:This time it gives Java.lang.null pointer exception.
At jsp,I implemented the changes you have told.Now if I check the values of arr using alert, the values are seperated by comma. But then at servlet it is giving null pointer exception.
where you put alert.when this ajax happening?when you cal this functons.Kindly tell clear ly
After pushing the values to arr, I used alert(arr).It showed all the values like apple,jam,mango. It is ok here. But now at servlet side, exception is getting thrown when I use list[0].
@edge check with updated code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.