• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

retriving an array in javascript

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic