• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is best apprach parse JSOn then send for JSP as Model or vice versa?

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am calling from my JSP page to server for JSON.I have to display that data in my JSP page.
I have 2 approach for this

1)GetParameter from JSP->Send To Controller->Get Data From DAO and make JSOn->Forward JSON to JSP -> Parse JSON in JSP


2)GetParameter from JSP->Send To Controller->Get Data from DAO and make JSON->Parse JSOn in Controller and Set attribute in a Model Object->Send Model Object to JSP->Display data from Model t JSP


Which approach I should use?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you be parsing JSON on the server? How are you "calling" to the server for the data? Ajax? Other? You haven't given us much information to go on.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 2 cents:
In java code, use java objects.
If you are passing it to a JSP as an attribute, forget the JSON step pass it as a Model Object
Only convert to JSON if you are sending a JSON response - which you would for an ajax call. And then the javascript engine on the page will parse the JSON.
The only time you should be parsing JSON on the server IMO is when the client has sent you some as part of a request.

Actually my process would probably be more along the lines of
Get data from DAO --> Model Object --> Convert to JSON if needed for response.







 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear,@Stefan
Thanks for reply


Why would you be parsing JSON on the server? How are you "calling" to the server for the data? Ajax? Other? You haven't given us much information to go on.


there is a page named myprofile in my web application I am thinking
get UserId from session in controller->get data based on that id from DAO->convert it as JSON response->send data to JSP->parse JSON response using AJAX in JSP

Will it be fine?

 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify then.

There are twp approaches you could follow here.
#1: Generate a page, fill it with data, send it back as a complete response. Pure Java/JSP. No need to use JSON as everything is handled on the server.
#2: Generate a page with no data. On loading the page, make an ajax request to load the data for the page. Use javascript to update the page with the values recieved over AJAX.


What you have described probably fits with #2 here, so should be fine. Just be aware that parsing the JSON response "using AJAX in the JSP" can't actually involve any java/jsp code.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan

I am also using Second approach
 
Dave Tolls
Ranch Hand
Posts: 2274
23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
there is a page named myprofile in my web application I am thinking
get UserId from session in controller->get data based on that id from DAO->convert it as JSON response->send data to JSP->parse JSON response using AJAX in JSP

Will it be fine?



This is a terminology thing.
You are not, in fact "send(ing) data to JSP", or "using AJAX in JSP".
You are sending it to the client, which is HTML/Javascript by that point.
JSPs execute on the server to generate a page.

It's quite a good idea to keep these things separate in your head as it can lead to mistakes in your code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's not clear to you, please read this article.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear

Now its clear Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!