java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.applet arrow Pass an Array between Java and Javascript
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4086
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Pass an Array between Java and Javascript E-mail
User Rating: / 26
PoorBest 

Javascript can read directly a Java Array but the other way don't seem be true. NOTE : Reading a Java array from Javascript may crash your browser for some unknown reason. So it's safer to use the next technique!

A safe and simple way is to transform the Array as a big String with a known character used a separator. From there, it's trivial to do some manipulations to retrieve the array.

In the following example, the first button is used to read directly the Java array. The second button call a Java method which to transform the array as a string, then the Javascript function split() is used to retrieve the array. The third button will modify the Java array. A Javascript array is transformed with the function join() and on the Java-side, a StringTokenizer will do the rest.

[Java applet]
import java.awt.*;
import java.applet.*;
import java.util.*;

public class TestJavaArray extends Applet{
 public String  javaArray [] =
   "array 1""array 2" "array 3" };

 public String [] getJavaArray() {
   return javaArray;
   }

public String getJavaArrayAsAString() {
   // you need more error checking here, of course...
   int k;
   String s = "";

   k = javaArray.length;
   s = javaArray[0];
   for (int i= ; i < k; i++) {
      s += "|" + javaArray[i;
      }
   return s;
   }

 public void putJavaArray(String arrayAsAString) {
   int i = 0;
   String s;
   StringTokenizer st =
      new StringTokenizer(arrayAsAString, "|");

   while(st.hasMoreTokens()){
     javaArray[i++= st.nextToken();
     }
   }
 }

[HTML and Javascript]
<HTML><HEAD></HEAD><BODY>
<SCRIPT>
 function getJavaArray() {
    arrayFromJava = document.myApplet.getJavaArray();

    alert("Java Array length = " + arrayFromJava.length + "\r\n" +
          "element 2 is " + arrayFromJava[1]);
    }

function getJavaArrayAsAString() {
    var arrayAsAString =
       document.myApplet.getJavaArrayAsAString();
    realJsString = arrayAsAString + "";
    arrayFromJava = realJsString.split("|");
    alert("Java Array length = " + arrayFromJava.length + "\r\n" +
          "element 2 is " + arrayFromJava[1]);
    }

 function putJavaArray() {
    arrayFromJs = new Array("ARRAY 1""ARRAY 2""ARRAY 3");
    arrayAsAString = arrayFromJs.join("|");
    document.myApplet.putJavaArray(arrayAsAString);
    }

 </SCRIPT>
 <FORM>
 <INPUT type="button" value="get JAVA array"
    onClick = "getJavaArray();">
 <INPUT type="button" value="get JAVA array (as a string)"
     onClick = "getJavaArrayAsAString();">
 <INPUT type="button" value="put JAVA array"
    onClick = "putJavaArray();">
 </FORM>
 <APPLET CODE="TestJavaArray.class"
         NAME="myApplet"
         HEIGHT=WIDTH=0>
 </APPLET></BODY></HTML>

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.