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.

edge
  • 257
  • 4
  • 20

1 Answers1

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[]");
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • :This time it gives Java.lang.null pointer exception. – edge Apr 12 '14 at 05:32
  • 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. – edge Apr 12 '14 at 05:43
  • where you put alert.when this ajax happening?when you cal this functons.Kindly tell clear ly – Sridhar R Apr 12 '14 at 05:49
  • 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 Apr 12 '14 at 05:53
  • @edge check with updated code – Sridhar R Apr 12 '14 at 05:53
  • Well,it is the same code that I used.No change in problem status. Because list.length is giving null Exception. – edge Apr 12 '14 at 05:58
  • @edge so problem is array not posted to servlet.getParameter only works if the parameter you are accessing exists in the URL that was sent to the servlet. Are you sure that the array is being sent to the servlet? If the url is this : `"/jspservlet?name=blah&myArray={1,2,3}"` (the curly brackets would be replaced with the correct url encode for brackets which would be %something), then using `request.getParameter("myArray")` would return the array. – Sridhar R Apr 12 '14 at 06:00
  • Using AJAX, No URL parameters are shown in url. I am not sure the array is going to servlet. Then what to do? – edge Apr 12 '14 at 06:08
  • @edge check with above updated ajax and servlet options – Sridhar R Apr 12 '14 at 06:15
  • @Which one is working last one ? – Sridhar R Apr 12 '14 at 06:20
  • yes the last one. Only this statement needs to be changed at servlet side: getParameterValues("List[]"); and of course the array push method as told by you. – edge Apr 12 '14 at 06:51