Click to See Complete Forum and Search --> : Returning array to JavaScript from Java Applet
TerryS
September 28th, 2005, 12:33 PM
Hi,
I have a question for which I cannot find an answer and would be very grateful for some help.
I have a Java Applet within an HTML page and I can call the Applet's methods from JavaScript without a problem when the page is viewed in Internet Explorer 6.0. If an Applet method returns any Java primitive data type then the value can be assigned to a JavaScript variable and displayed (and this is also true if the Applet method returns a Java String). However, if an Applet method returns a Java array then the JavaScript variable to which it is assigned is always valueless.
So, with Internet Explorer 6.0 is it actually possible to have an Applet method that returns a Java array to JavaScript? If so, could someone please provide a simple example.
I know that the Java array could be converted to a string before returning to JavaScript, however I was hoping to avoid this because one of the arrays I wish to return to JavaScript from the Applet is a byte array which I then wish to pass to an ActiveX method which will accept the byte array as a variant data type. Thus ideally I would like the JavaScript to accept the Java byte array as a sort of 'blob', the contents of which I do not not need to access in the JavaScript itself.
Many thanks.
Terry.
Comike14
September 28th, 2005, 03:52 PM
Hi there,
You sound like you already know what you're doing, and what needs to be done, so I'm not sure how much help this is going to be...
You probably already know that JavaScript and Java really are only related in name and some syntax, but other than that there is no real connection. Java, including the web applet stuff, is all strongly-typed, while JavaScript stores all of its objects in the same way initially. You see, elements in a Java array can be considered to have different types (I don't remember if you can have an array with a String, int, and boolean in the same array, but I do know that you have have an array of ints, one of strings, etc.)
JavaScript on the other is going to store all of its elements as strings, and as you know, a different number of bytes is allocated for a String than for an integer. Therefore if you try to send an array from Java to JavaScript, and it's of anything other than string type, you'll get a meaningless collection of either trash data, or no data at all because the JavaScript doesn't really know that the elements of the array are supposed to by 16 bytes, as opposed to 32 bytes, or whatever.
As far as I know, the conversion to a string is the only real way to do what you want. Maybe someone with more experience with these things can chime in. And even that might not work, it depends on how many bytes Java considers a string to be compared to how many bytes a string is in JavaScript... Good luck, and post up here what your solution is (I'd like to learn, too)
EDIT: also consider that in most strongly-typed languages, arrays are passed by reference by default. Maybe if you could dynamically iterate and pass each individual array element into a new JavaScript array, as some sort of modified copy constructor?
ie.
var nuArray = new Array();
for(int i = 0; i < numElements; i++) nuArray[i] = getArrayElementValue(i);
Depending on the size of your original array, this could be a very simple fix. Obviously if you're working with thousands+ of elements, you wouldn't want to mess with that approach.
PeejAvery
September 28th, 2005, 05:07 PM
Like Comike14 mentioned, there are only a few attributal associations between the two. Could Rhino (http://www.mozilla.org/rhino/) possibly help to you?
TerryS
September 29th, 2005, 07:23 AM
Thank you both for your helpful suggestions.
Comike14, I will consider your comments about JavaScript data storage - they are useful because I was increasingly 'unable to see the wood for the trees', and your comments have helped refocus my attention on what is the fundamental problem - data storage mis-match.
In this regard the constraint seems to be Internet Explorer and its failure to support LiveConnect - with Netscape (and possibly FireFox, I'm not sure) the JavaScript supports additional LiveConnect data types, which include the 'JavaArray' type to represent Java arrays! If IE JavaScript included 'JavaArrays' I don't think that I would have a problem.
Your idea of iterating through the array elements is a possibility which I will consider, however my arrays are, unfortunately, quite large so I'm not sure that this would be a viable solution in practise.
Peejavery, your suggestion of using Rhino is interesting. I have never used Rhino but have just had a quick look at the Mozilla website and Rhino does appear to offer some of the capabilities of LiveConnect, such as allowing core Java classes to be accessed from JavaScript. However, I'm not sure how I would use Rhino and, more critically, can it actually be used with Internet Explorer? (Unfortunately IE is a requirement of the project, so I cannot change the browser.) I will investigate further.
Again, many thanks.
Terry.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.