0

I need to pass this below array [aLocations] from jsp.

<script type="text/javascript">
  var aLocations = new Array();
  var aTitles = new Array();
  var aDetails = new Array();

  aLocations = ['keswick,cumbria,uk','grasmere,cumbria,uk','ambleside,cumbria,uk'];

</script>

if i tried like this, it is not working:

<%
ArrayList<String> myArr = new ArrayList<String>();
        myArr.add("keswick,cumbria,uk");
        myArr.add("grasmere,cumbria,uk");
        myArr.add("ambleside,cumbria,uk");
%>

<script type="text/javascript">
  var aLocations = new Array();
  var aTitles = new Array();
  var aDetails = new Array();

  aLocations = <%=myArr%>;

</script>

what is the best approach to pass from JSP?

Gnaniyar Zubair
  • 8,114
  • 23
  • 61
  • 72

1 Answers1

0

You need a library. For example with http://code.google.com/p/google-gson/ you can:

Gson gson = new Gson();
aLocations = <%=gson.toJson(myArr)%>;

or see https://sites.google.com/site/gson/gson-user-guide for documentation.

Without a library you need to manually create a structure like ["imtem1","item2",..,"itemn"] with iteration, string concatenation, escape handling, etc. See Populating JavaScript Array from JSP List

Community
  • 1
  • 1
Luca
  • 4,223
  • 1
  • 21
  • 24