JSP/Javascript and arrays

Hi all, I have build and initialized an array inside jsp code (<% ... %>). What i would like to know is how to get this array inside a javascript function (by passing it as an parameter or another way).Thanks in advance
[246 byte] By [PirateManiaca] at [2007-11-26 16:25:23]
# 1
If you want an array available to javascript, you'll have to output it as a javascript array just like you output your javascript functions. Here is a tutorial explaining the basic syntax that you should use: http://www.w3schools.com/js/js_obj_array.asp
gimbal2a at 2007-7-8 22:49:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> If you want an array available to javascript, you'll have to output it as a javascript array

Specifically, Java and Javascript do not directly communicate. JSP is processed on the server before the page is sent to the client (browser), and the javascript is processed once the page is rendered by the client (browser).

In your JSP page, if you want to get the contents of your Java array into a Javascript array then a simple solution is to output the contents of the Java array as arguments for the Javascript array.

Example

<%

String[] javaArray = {"one", "two", "three"};

out.println("<script>");

out.print("var jsArray = new Array(");

int i = 0;

for (i = 0; i < javaArray.length; i++)

out.print("\"" + javaArray[i] + "\", ");

out.println("\"" + javaArray[i] + "\")");

out.println("</script>");

%>

Page Source in Browser Will Be...

<script>

var jsArray = new Array("one", "two", "three");

</script>

Zoan333a at 2007-7-8 22:49:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...