This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

retriving an array in javascript  RSS feed

 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Robert
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!