0

Below is the code in my jsp file to convert Java's ArrayList to JavaScript's Array:

<% for(int k=0;k<listDate.size();k++){%>
    var temp =<%=listDate.get(k)%>
    dates[<%=k%>] = temp;
<%}%>
document.write(dates[0]); // it prints out a totally unrelated value which I have no idea what it is "1974".
Tunaki
  • 132,869
  • 46
  • 340
  • 423
kylas
  • 1,425
  • 6
  • 24
  • 38

1 Answers1

0

This is just to give you a reference how you can update the code and used the scriptlet result --

<%

String hiddenResult = "";


// DO your business logic here and assign value to the  hiddenResult
<% for(int k=0;k<listDate.size();k++){%>
     hiddenResult = hiddenResult + listDate.get(k)+",";
     hiddenResult = hiddenResult.substring(0,hiddenResult.length()-1);  
%>

Assign the scriptlet result in the hidden field of the form and use that hidden field like this

<input type="hidden" id="hiddenInputResult" name="hiddenDDResult" value="<%=hiddenResult%>" />
<!--assign the value here in hidden field -->



function myFunction(){
    var arrayValue = document.getElementById("hiddenInputResult").value;
    // you can split by using arrayValue.split(","); to use

}
Narendra Jaggi
  • 1,297
  • 11
  • 33