• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to pass Array from a servlet to jsp in response to javascript call.

 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arraylist in my servlet which i need to pass to jsp as a response for a javascript call. This is an arraylist of DTOs. There are 24 DTO objects in the list. The DTO has an int variable and a HashMap. And i need to pass this into the javascript code in my jsp page. Do i use json? Do i send the entire list as a json object or should i iterate through the arraylist (from servlet) and serialize all the DTOs and pass that to the jsp? Also please add a few links to tutorials that i could use. Thnks.
 
Dave Tolls
Ranch Hand
Posts: 2282
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what you're doing with the data, but usually you'd turn the thing into a JSON string and return that.

It's possibly slightly pedantic of me, and I expect others don't agree, but at the point this is going on it is not a JSP. It is a page on the client.
Talking about "pass to JSP" implies, to me, that you are still on the server and simply forwarding to the JSP.
This is not what you are doing. You are sending a response to the client.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave raises an important point. Servlets and JSPs (after all, a JSP is really a servlet) are processed on the server side. The JSP sends plain old HTML to the client so by the time you are dealing with the client side, JSP is done, over, finished. When dealing with the client side of things, you must think in terms of HTML, JavaScript, and CSS. On the client side it's not a JSP page anymore, it's just a HTML page.
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I iterated through all all of it using nested loops and converted the entire thing into a string. The rest of the output will be formatted using basic html. It is working fine now. Suggestions??
 
Dave Tolls
Ranch Hand
Posts: 2282
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use one of the existing JSON libraries for turning objects into JSON strings.
It'll be more reliable than rolling your own.
GSON, Jackson, etc etc. There are several.
 
Junilu Lacar
Sheriff
Posts: 10025
120
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest that if you are able, post a sample of what you're doing in your JSP. Then we can provide more concrete feedback. You could be in the minority but based on percentages, it would be a good bet to guess that what you're doing now that works is not the ideal approach.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!