[Solved] Passing an array from Java (JSP) to jQuery


Use a JSON library for Java. Then serialize your ArrayList to JSON:

ArrayList wordslist = new ArrayList();
wordslist.add("Hello");
wordslist.add("again");

String json = (new JSONArray(wordslist)).toString();

Echo the JSON text at the appropriate place in your JavaScript code. E.g.

$(document).ready(function(){    
    var arr = <%= json %>;
    $.each( arr, function(i, l){ 
        alert( "Index #" + i + ": " + l );
    });
});

I’m actually not sure whether this is the right JSP syntax for printing a variable. Please correct if not.

2

solved Passing an array from Java (JSP) to jQuery