0

i need to pass an arraylist to javascript from jsp and then iterate through the list in javascript. Can someone help me with the jsp synatax as well as javascript iteration code?

TIA

2
  • 1
    Please show the code you've tried. Commented Aug 26, 2013 at 5:17
  • 1
    why you want to iterate, if to show a list on the page you can use logic:iterate and jsp display tag Commented Aug 26, 2013 at 5:18

1 Answer 1

1

Try this code:

ArrayList list = new ArrayList();
list.add("aaa");
list.add("bbbb");
list.add("cccc");
Iterator individualItems = list.iterator();
int i = 0;
String script = "<script type=\"text/javascript\">var list = new Array();";
while(individualItems.hasNext())
{
    script += "list["+i+"] = \""+individualItems.next()+"\";";
    i++;
}
script += "for(i=0;i<list.length;i++){document.writeln(list[i]);}";
script += "</script>";
//now do whatever you want with it
PrintWriter output = response.getWriter();
output.println(script);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.