0

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>
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..

4
  • Thejsp variable should be declared as <%= ArrayList myList=(ArrayList)request.getParameter("list_name") %>. Commented Jan 13, 2011 at 12:38
  • Hi Alaa, Thanks for your quick response. Commented Jan 16, 2011 at 4:04
  • Certainly i have declared my variable like this as this is the usual way of declaring jsp variable as you are saying. <%= ArrayList myList=(ArrayList)request.getParameter("list_name") %>. Commented Jan 16, 2011 at 4:06
  • My problem is now how i can use that jsp variable in my javascript, this list is having thousands of records. Commented Jan 16, 2011 at 4:08

1 Answer 1

1

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>
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.