Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array from jsp to JavaScript

 
Soumya Ghosh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have to pass an array from a jsp scriplet to a JavaScript function.
In jsp array is populated by items collecting from database.Then I have to pass it to javascript and retrieve those items within JavaScript.
How can I do that?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is that you can't. The JSP runs on the server to create the HTML page, and then the Javascript runs on the client. There can be no direct passing of the data from one to the other. (See this article for more information.)

But what you can do is to use the JSP to construct an equivalent Javascript construct on the page that the Javascript code can reference.
[ January 12, 2007: Message edited by: Bear Bibeault ]
 
Soumya Ghosh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I construct an equivalent Javascript construct on the page that the Javascript code can reference?
Can you please tell this using a small demo code?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will not write the code for you. You will learn more that way. But I will help you along.

Let's say it's a string array. In Javascript, a string array could be constructed with:



So you'll need to write code in your JSP to construct such syntax using the elements in the Java array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic