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".
5
  • what do you store in the listDate? What should be printed, what actually gets printed? How does the resulting javaScript code look like? Commented Dec 30, 2015 at 11:20
  • i found the solution: var temp ="<%=listDate.get(k)%>"; Commented Dec 30, 2015 at 11:22
  • While, its better to create JSONArray on server-side then parse it in javascript. Commented Dec 30, 2015 at 11:24
  • stackoverflow.com/questions/17359830/… Commented Dec 30, 2015 at 11:24
  • Your code might be breaking, wrap your scriptlets in single / double quotes, if you want your current solution to work. Commented Dec 30, 2015 at 11:24

1 Answer 1

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

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