user649716
user649716

Reputation: 109

How to transfer large JSON Object from servlet to redirected HTML

What I am stuck on is how can I transfer very large JSON from servlet to html?

I have a servlet which does some processing and stores this in JSON. A new html page is made for the user (redirected), which requires the JSON to be passed somehow for Javascript to loop through and display in a presentable manner.

I am not 100% required to use JSON, just anything that can transfer 200ish rows of two distinct 'columns' of data; this of course needs to be easily accessible using Javascript at the other end.

Thank you

Upvotes: 0

Views: 1651

Answers (2)

djna
djna

Reputation: 55897

If you are generating a new HTML page then you can simply embedd the data in a script tag within the page.

More usually we serve up an HTML page which loads a script, that script makes a request to the server, the request returning JSON which is then processed by the script, which will adjust the displayed DOM to show the data.

If you're using a framework such as Dojo then the code to do this is pretty easy, see examples such as this

Separating out the retrieval of the data from the specific page tends to work out well in the long-run: you then have a data service you can reuse in many pages.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691685

If I understand correctly, you have

  1. first request : server generates JSON and sends a redirect to the client for another page
  2. second request (to the page in the redirect) : client loads the HTML, and should have access to the JSON object generated in first request.

If this is correct, you need to store the JSON somewhere (in the session, for example), at the end of the first request, and then you have two choices :

  1. make a third AJAX request from the HTML page to a servlet to get the JSON from the session
  2. embed the JSON inside the HTML sent to the client at the second request (inside a <script> tag, in Javascript variable initialization)

Upvotes: 1

Related Questions