• 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 i write in a servlet response an javascript array?

 
Greenhorn
Posts: 20
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How i write in a servlet response an javascript array?
 
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
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic