File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes passing java array to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "passing java array to javascript" Watch "passing java array to javascript" New topic

passing java array to javascript

sihani rani
Ranch Hand

Joined: Jun 22, 2010
Posts: 32
i have String array in jsp ..
how can i pass that String array to java Script function

this is my java code:


String[] s=new String[10];

s[1]= "orange";

<body onload="???"> // how can i pass string array 's' to onload function

this is my java script code

function(???) // how can i get String array 's' from onload function


please answer me..

thanks in advance..

Nilesh Miskin
Ranch Hand

Joined: Jun 17, 2010
Posts: 44
Retrieving template text with JavaScript sounds a weird requirement to me. You can evaluate other design options to fulfill your goals.
One more thing,
String[] s=new String[];

Above statement won't compile as you haven't specified the dimension(size of the array).

Nilesh Miskin
Sean Clark

Joined: Jul 15, 2009
Posts: 377


You can't pass the array directly you'd need to translate it into into javascript. You could use JSON or create a custom taglib to do it for you. Also having code scriptlets in your JSP is not really a good idea.


I love this place!
David Newton

Joined: Sep 29, 2008
Posts: 12617

There's no reason to pass the Java array to the JavaScript function: define the array in JavaScript.

If that's not the problem you're *actually* trying to solve, then TellTheDetails.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: passing java array to javascript
It's not a secret anymore!