• 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 to copy java Object Array into javascript Array (of Objects)

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am fairly new to javascript objects. I have a java object array. I have to copy this array into javascript object array, to print as a table.

So far, I have this:

var jsArray = <%=lineArray%>;

and this doesn't work...

lineArray is an array of java objects.

Can anybody let me know how to accomplish this...

Thanks a lot!

 
Sheriff
Posts: 67747
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

srinivas chary wrote:var jsArray = <%=lineArray%>;


Of course that doesn't work. All that is going to do is to spit out the .toString() result of the array into the HTML stream. Not a useful thinkg to do. Look at the HTML being sent to the browser. Is it what you expected?

Remember that a JSP is just a template that runs on the server in order to create an HTML page to be sent to the browser. Please read this article to understand how JSP operates.

JavaScript and Java cannot interact. They run on completely different machines at completely different times. Rather, you use JSP to create the JavaScript code that recreates the data in JavaScript syntax.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with what you just said. I already know that.

JSP returns the array, which am storing in the session. I can print the size of array using "alert" .

Problem is how to print this array of objects in a table using JavaScript. Is JSON the way to go? Can anybody point to a simple JSON example...

Thanks



 
Bear Bibeault
Sheriff
Posts: 67747
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
Sure, JSON would make this relatively easy as it can represent a JavaScript array nicely.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could copy a simple java array to javascript array:

I followed this logic



http://forums.devshed.com/javascript-development-115/passing-array-from-jsp-to-javascript-53627.html

Is there such a simple way to copy Objects aswell?
 
Bear Bibeault
Sheriff
Posts: 67747
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
Scriptlets? In 2010? Why are you not using the JSTL and EL?
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Scriptlets? In 2010? Why are you not using the JSTL and EL?


The problem is not the JSP itself.

Does JSTL & EL allow me to do this

with Objects?
 
Bear Bibeault
Sheriff
Posts: 67747
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
Much more easily.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem boils down to declaring an array, something like this:



but with custom objects instead of strings.

Can anybody share syntax for declaring an array of custom objects (similar to Pojos)...
 
Bear Bibeault
Sheriff
Posts: 67747
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
You need to figure out how to represent the objects in JavaScript notation, and then write a JSP to produce that notation.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what solved my problem:

http://stackoverflow.com/questions/1290131/javascript-how-to-create-an-array-of-object-literals-in-a-loop

P.S. Don't reply if your intention is to hijack the thread or mislead or mock.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic