• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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: 67532
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: 23553
161
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.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic