wood burning stoves 2.0*
The moose likes JSP and the fly likes How to pass Object to Javascript function in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass Object to Javascript function in JSP" Watch "How to pass Object to Javascript function in JSP" New topic
Author

How to pass Object to Javascript function in JSP

Tony Ghy
Greenhorn

Joined: Feb 12, 2011
Posts: 8
I'm trying to dynamically populate a div in my jsp.

I've a list of objects (which is nothing but a Map<String, Object>)

The following code works:



But I need to pass the object as it is so that I've better control of what should be inserted in the 'panel_info' div.

Hence I need to send myObject to doSomething() somthing like :



But when I do this is displays : undefined.

Please guide me on how i can pass it as an object.

Please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60763
    
  65

Look at the HTML that is sent to the browser, is it what you expect? Does it look right?

Remember that the JSP is executing on the server to create the HTML page to be sent to the browser. So Java objects can never be passed to JavaScript.

Maybe this article will be helpful.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tony Ghy
Greenhorn

Joined: Feb 12, 2011
Posts: 8
Bear Bibeault wrote:Look at the HTML that is sent to the browser, is it what you expect? Does it look right?

Remember that the JSP is executing on the server to create the HTML page to be sent to the browser. So Java objects can never be passed to JavaScript.

Maybe this article will be helpful.


Oh yeah...silly of me. I got your point.

So the only solution would be to pass some ID to the Javascript and then do an Ajax call to fetch the required data without reloading the page.

Okies...my motive is solved. Thanks for the direction. Have a nice day!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60763
    
  65

Or, use the JSP to create a JavaScript object with the same properties. JSON is an easy way to handle that,
Tony Ghy
Greenhorn

Joined: Feb 12, 2011
Posts: 8
Bear Bibeault wrote:Or, use the JSP to create a JavaScript object with the same properties. JSON is an easy way to handle that,


Yes,I've used JSON to fetch some of the data in my jsp. (e.g. I've used JSON to hold the city names list for a particular statename)

But for this case, I don't want to fetch all the data in some JSON at a go. If the user clicks on the 'more details' link, it should fetch the data...so Ajax seems better in this case. What say?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60763
    
  65

Ah yes, if it's the result of an event, it's probably better to fetch the data as it is needed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass Object to Javascript function in JSP
 
Similar Threads
How can I call servlet from jsp without using form
location.href not working on FireFox 3.5
wrestling with json
Doubt on Tags
DIV Tag Loading Behavior is Erratic