Click to See Complete Forum and Search --> : pass server objects to client
lakshman
05-09-2003, 02:15 AM
Hi All,
Is my understanding right...!!
I can't pass server side object to client side script.
If yes then, does that also include Arrays. The reason I am asking is that there are arrays in Javascript & Java. So will JS know how to re-construct an Array that is sent from a servlet.
If yes a small example will be good.
If Not then is my understanding correct that we can ONLY pass a string object via session.
Thanks
khalidali63
05-09-2003, 07:36 AM
Originally posted by lakshman
correct that we can ONLY pass a string object via session.
You can only pass strings via Http Request,because thats what a browser will understand to be displayed by javascript.
JavaScript has no idea what will a session object be.
As mentioned above,the best bet you have is to create an array using jsp.syntax for doing that (loosly) will be like this
<script type="text/javascript">
<%
String[] sArr = get your array here;
for(int x=0;x<sArr.length;x++){
%>
var sArr = new Array('<%=sArr[x] %>');
<%
}
%>
The code is not optimised nor its tested,may have tiny bits of syntactical errors.
lakshman
05-09-2003, 09:36 AM
Thank you both,
I am currently returning a huge string with a pre determined delimiter char. Then breaking it up using the delimiter and re-constructing the array.
For eg.
// Get the whole string
var applString ="<%=customerBeanId.getSrchResultString() %>";
// Split it by records.
var listOfAppl = applString.split("|");
<script type="text/javascript">
for (i=0; i < listOfAppl.length && applString.length > 0; i++)
{
// Split each record into fields. and display in a table or
// what ever.
applDetails = listOfAppl[i].split("~");
.
.
.
.
}
</script>
I just thought there was a clean way to do it. Like passing the server side object. Oh well, just have to wait till the browsers catch up to doing this kind of stuff.
I think khalid's idea is a bit better because we do not have to depend on a pre-determined delimiter.
Dave your code on sorting using Array within Array is a neat trick. I can see endless possibilities.
Thanks again to both of you, You are a wealth of knowledge.
lakshman
05-10-2003, 05:42 AM
Hi All
Does any one know a neat trick, similer to khalids creating of JavaScript Array from a Java String Array (Ref : above), But to pass JS Arrays to a Servlet.
One Method I know is to Construct the JS Array into One BIG string and pass it to Servlet. On the Servlet End reconstruct the array.
In short, Can I pass a JS Array to Servlet and retrieve it in Servlet as an Array.
Thanks one and all.
Charles
05-10-2003, 12:27 PM
Or you could simply use the array as a string. That will automatically call Array.toString() which is the same as Array.join(',') unless you have used the 'language' attribute in your SCRIPT tag. In that case Netscape will do something else.
<script type="text/javascript">
<!--
alert (['fee', 'fie', 'foe', 'fum'])
// -->
</script>
khalidali63
05-10-2003, 01:11 PM
Ohkkay here it goes,
I have updated the link below with updated java applet.
In the text field whre you can enter send to applet text,enter a string with spaces,
I use the String.split(' '); in the javascript section to create an array,and pass it to the java applet class where this array is looped through and a , (comma) is added, finally the text when it appears in the applet text field its appended with comma's
.This is the only possible solution to your problem,since javascript and servlet are completely different things to interact with each other.(and as of today they don't)
Let me know if you have any questions about the applet.
http://68.145.35.86/skills/javascripts/applets/AppletJavaScriptInteraction.html
EDIT:
I just tested it with IE6+,its breaking up on me..It works with NS6+ though,
IE is doesn't seem to like the array param on my machine for some reasons..
I'll work on it when have nothing better to do..:-)
lakshman
05-10-2003, 09:41 PM
Hi All,
Thanks for that, you have all been very help full yet again.
You NEVER fail to disappoint me...!!!! I very much appreciate your efforts.
Thanks again.