Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How i write in a servlet response an javascript array?

 
Frankely Diaz
Greenhorn
Posts: 20
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How i write in a servlet response an javascript array?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without more details, hard to say. If you are emitting from the servlet (not recommended), simply use print statements like the rest of the markup.

Recommended would be to create a scoped variable containing the data and to forward to a JSP in which the JSTL and El would be used to generate the necessary JavaScript markup.
 
Daniel Val
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankely Diaz wrote:How i write in a servlet response an javascript array?


Hi,

Rather than doing just the array, creat a JSON object string containing an array, like that

{arrwhatever: ["first val", "second val", "third val"]}

- Be careful to ensure that the object can be parsed by the client. How you do it: load on the client the string in a variable, let's call it alfa, and then say

var beta = eval ('(' + alfa + ')') and you got the Javascript object, and beta.arrwhatever would contain your array

- This is typical for Ajax, this is what you try to do, right?

- This construct gives you flexibility. Maybe the servlet call crashes, in this case instead of arrwhatever you load another attribute called error in which you pass whatever error happened.

Regards,
Daniel
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to anything with eval() -- which should be avoided in almost all cases. The JavaScript notation can be written directly into the markup so no evaluation is needed.
 
Daniel Val
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There is no need to anything with eval() -- which should be avoided in almost all cases. The JavaScript notation can be written directly into the markup so no evaluation is needed.


Right, but I don't believe he wants to use a servlet in order to write this into the markup; most likely he is placing an Ajax call in which case the servlet returns the pure data or at least from his message he mentioned he wants the servlet to return only the JS array...

D
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any evidence that the OP is using Ajax. In fact, the post os sop cryptic it's hard to tell at all.

Even so, eval() should be avoided. There are better says to parse JSON.
 
Paul Clapham
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Rizwan,
Your post was moved to a new topic.
(Nothing at all to do with writing Javascript from a servlet)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic