• 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

How do I pass a Java bean list to JavaScript?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you pass a server-side bean value or list of values to JavaScript in a <script> block?

So, I have this simple Java bean:



In my servlet application I create a list of these things.


Now in the JavaScript I want to plot them.  For simplicity in this example I'll just log them.



In my final code I'll want to plot those points on a canvas using SVG.  My aim is to write a simple web program to upload plot
files and display plots.  But I have no idea how to reference Java class types from JavaScript.

 
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
Remember that all JSP does is to "write" the HTML document to then send to the client (refresher: this article).

So, to make your data available to the JS on the page, you need to recreate the data there. There's no direct passing of the data.

An alternative (which I'm not advocating at this time), is to obtain the data via an Ajax call, where you would pass the data as a JSON construct.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript and Java are not related. Java beans only exist within a Java VM (on the host, since applets are DOA), JavaScript is typically running on the client (since there's no JVM for server-side JavaScript).

However, there are mechanisms for serializing JavaBeans for JavaScript to consume - just marshall the bean out to JSON (Javascript Object Notation). JavaScript can easily consume JSON data and convert it to JavaScript equivalent data structures. Likewise, JavaScript scan write out JSON and send it to a server for a Java backend to parse into POJOs.

In fact, that's the heart of how you get data back and forth in AJAX. Although other formats also work (like SOAP and YAML), JSON is the most JavaScript-friendly encoding mechanism, And I believe that there's currently JSON libraries built into the stock JVM. Or if not, several alternatve third-party libraries.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic