aspose file tools*
The moose likes Servlets and the fly likes How i write in a servlet response an javascript array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How i write in a servlet response an javascript array?" Watch "How i write in a servlet response an javascript array?" New topic
Author

How i write in a servlet response an javascript array?

Frankely Diaz
Greenhorn

Joined: Oct 09, 2009
Posts: 20

How i write in a servlet response an javascript array?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61217
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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

Joined: Jan 09, 2012
Posts: 44
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Muhammad Rizwan,
Your post was moved to a new topic.
(Nothing at all to do with writing Javascript from a servlet)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How i write in a servlet response an javascript array?