Getting ArrayList object from servlet and storing it as javascript array variable using AJAX
I have some arrayList objects as my servlet request attribute. I want to get it into my javascript variable which is in a JSP page. I tried like this.
abc.jsp
<script&g开发者_如何学Got;
var myList=<% (ArrayList)request.getParameter("list_name") %>;
//do use of myList.....
</script>
But this is not working. I am not getting the data.
Then tried with
var myList=<% =(ArrayList)request.getParameter("list_name") %>;
Didnt work!!
Thanks in advance..
Since the toString()
method of ArrayList
would accidentally give the desired result, then you can simply use var myList = ${list_name};
. But the result of your 2nd snippet should also be working, so I would assume that you don't have the list set as a request attribute.
Make sure you've:
- called
request.setAttribute("list_name", yourlist);
in the servlet - used forward, rather than redirect to the jsp.
You can also try [${fn:join(list_name, ',')}]
or
var myList = new Array();
<c:forEach items="${list_name}" var="item" varStatus="loop">
myList[${loop.index}] = "${item}";
</c:forEach>
精彩评论