aspose file tools*
The moose likes JSP and the fly likes want to access an arraylist of jsp in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "want to access an arraylist of jsp in javascript" Watch "want to access an arraylist of jsp in javascript" New topic
Author

want to access an arraylist of jsp in javascript

amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
hi all,
i am getting an arraylist in jsp.(scriptlet). i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.
please guide me how i can do this as we can do with strings.

please reply.

thank you.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

amit daundkar wrote: i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.

Simple you can't, Its not possible to pass an Java run time object to JavaScript, You have to set those arraylist values to some hidden input element and then access those in JS OR create arraylist in JS itself.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
Sagar Rohankar wrote:
amit daundkar wrote: i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.

Simple you can't, Its not possible to pass an Java run time object to JavaScript, You have to set those arraylist values to some hidden input element and then access those in JS OR create arraylist in JS itself.


offcourse sagar.thats how i do it normally.
that works fine with Strings but not withcollection object.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

amit daundkar wrote:
that works fine with Strings but not withcollection object.

Strings are universal, they are single entity that has not any complex structure and nay script can understand it, thus we can pass strings values to JS, but not the reference of a same.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just let JSP print it out as a JS variable. This way JS can access it when it is about to run.
amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
Bauke Scholtz wrote:Just let JSP print it out as a JS variable. This way JS can access it when it is about to run.


i did not get it bauke. i mean i didnt understand by printing it as a JS variable.
please explain.
thhank you.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

I think, what Bauke suggest is something like this,



And in JS



Correct me here, If I were wrong
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
No, I didn't mean that.

You know, JSP runs at the server machine and produces HTML/CSS/JS output.

Just let JSP print it out as a JS variable. Literally.


If SomeBean#getSomeProperty() returns for instance "foo", this would produce

which is perfectly valid JS after all when sent to the client side and running over there.

Using hidden input elements is whacky and plain cumbersome.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Bauke Scholtz wrote:
Using hidden input elements is whacky and plain cumbersome.

Thanks buddy, I didn't know that we can do something simple as that, what I used to do(very rarely) is something like I posted previously
amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
Bauke Scholtz wrote:No, I didn't mean that.

You know, JSP runs at the server machine and produces HTML/CSS/JS output.

Just let JSP print it out as a JS variable. Literally.


If SomeBean#getSomeProperty() returns for instance "foo", this would produce

which is perfectly valid JS after all when sent to the client side and running over there.

Using hidden input elements is whacky and plain cumbersome.


hey Bauke ,
thank you buddy.
but i can do it easily with a DWR call and asign a JS variable to a bean or a collection. and i have done it quite a few times. what i want to ask is,
i am getting the ArrayList in the scriptlet part of a JSP and not in a JS function. but for some reason i want to access this ArrayList in a JS function.
thats what i want to do.

i can get the same list in JS function with a DWR call but i dont want to do it as i need the list in the scriptlet part of JSP.
i hope you understand what i mean.
please reply.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

The answer remains the same.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: want to access an arraylist of jsp in javascript