Forums Register Login

retriving an array in javascript

+Pie Number of slices to send: Send
hello ranchers,

I am tyring to retrive an array in js, which i put as a request attribute.



i am trying to retirve it in js as follows


the alert gives me 26, which is wierd... am i missing something here?? is this not the way to get an array in js...

any help would be appreciated..
thanks
+Pie Number of slices to send: Send
Take a look at what is being sent to the browser (View Source). Is it what you expected?

If you are expecting the Java scriptlet to be evaluated when the JavaScript is executed, then you need to read this article to understand how JSPs operate.


+Pie Number of slices to send: Send
thanks for the response,

I looked at the source,


it seems like , when i say temp.length it is giving me the length of the string which i think, is the name of the object.

If you are expecting the Java scriptlet to be evaluated when the JavaScript is executed, then you need to read this article to understand how JSPs operate.



I am not trying to do this, All i am trying to do is, I want to retrive the values i have in a string array in the request scope in my javascript function.

Thanks,
+Pie Number of slices to send: Send
 

John Robert wrote:I am not trying to do this


I'm not sure what you mean by that; the article points out why you are getting the results that you are getting. Do you understand why the view source shows you what it does?

As the article points out, the scriptlet is evaluated on the server long before the page gets sent to the browser where the JavaScript executes. In order to obtain the array data in JavaScript, you need to write the JSP such that it emits a JavaScript construct that, when evaluated on the client, results in the JavaScript array.

In other words, create markup along the lines of:

By the way, Java scriptlets in JSPs are a thing of the past. You should be using the JSTL and EL in your JSPs.

And, as this is much more about JSP than it is about JavaScript, it's been moved to the JSP forum.
+Pie Number of slices to send: Send
My mistake, all i was trying to point out is, I was not expecting the scriplet to be executed, while my javascript is being executed. Sorry about that.
I tried what you suggested in your second message . It worked, but for this to work, I have to change the array to a String , before i put it in the request scope. May be i did not communicate it right, is there a way for javascript to understand an array created in java(as i understand it now, i have to change {"1","2","3"} to [1,2,3] for js to recognize this as a array).


Thanks,
+Pie Number of slices to send: Send
 

John Robert wrote:It worked, but for this to work, I have to change the array to a String , before i put it in the request scope.


Not necessary, and a poor way to approach the issue. Rather, send the array to the JSP as before, and use the JSP mechanisms (again, JSTl/EL preferred over scriptlets) to generate the desired markup that represents the JavaScript array construct.

May be i did not communicate it right, is there a way for javascript to understand an array created in java


You communicated fine, but the answer is still no. All the JSP does is to create a text buffer (which just happens to be an HTML page) to send to the browser. No Java-ness, or any other server-side language, makes it out of the server. Anything you want to express on the client has to be rendered as part of text markup sent as the response.
+Pie Number of slices to send: Send
Bear,
Now I see, why you pointed me to that article. Makes more sense now.I will look into JSTl/EL. Appreciate the help.

Thanks,
+Pie Number of slices to send: Send
No problem. If you run into trouble trying to figure out how to use JSP to render the text of the JavaScript from the array, you know where we are!
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3204 times.
Similar Threads
Problem in passing a String with '&' to URL
Serialization Question
SOLUTION! function that works in script tag but not in js file
dependent select boxes with ajax .
jvascript String object-replace
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:51:40.