Totally a newbie for jsp. In my project, I need to send an arraylist / array from java to javascript.
This is my java code in jsp.
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<% List<String> strList = new ArrayList<String>();
strList.add("one");
strList.add("two");
strList.add("three"); %>
in my javaScript, I want something like below.
$(document).ready(function() {
var notes = ["one", "two", "three"];
});
So how do I send the data from java to javascript? Please be specific. Thank you in advance.
solution from Bilal
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<html>
<body>
<% List<String> strList = new ArrayList<String>();
strList.add("one");
strList.add("two");
strList.add("three"); %>
<script type="text/javascript">
$(document).ready(function() {
var notes = new Array();
<%
for(String note:strList){
%>
notes.push('<%=note%>');
<%}%>
alert(notes[0]);
});
</script>
</body>