Parag Jadhav Parag Jadhav - 1 year ago 75
Java Question

Which is a good practice & choice :Sending a Hash Map object or a JSON from java servlet to jsp?

I am working on a java project/ web application with MongoDB. In this project I'm querying a Monogo database from a Java servlet. I am just confused whether to convert the query result to Json or a HashMap and send it to JSP or is there any other way to achieve this, and in the jsp file I can get the HashMap data and send it to the javascript in the same file.(I need to use the data in javascript)

My question/ doubt is:

  1. Will it be a good practice to send a HashMap with a large amount of data (somewhere in the range of 300,000 to 700,000 records for now) from servlet to JSP.

  2. Or should I create the JSON in servlet with same amount of data and send it to the JSP file, then parse the JSON in jsp and somehow access the parsed JSON in javascript code.

  3. Another way I could think of is to directly send the JSON from servlet to javascript and parse the JSON in javascript itself.

Which one will be a good solution in terms of Security, Performance?

I am new to this whole web application thing but I really need the help to clear the doubts in my mind. Also if you guys could direct me to some links/tips for optimizing the code for my web application that would be a huge help.

Answer Source

Definitely keep using the HashMap.

Serializing from HashMap to JSON and back is very expensive, and should only be used when you are transferring data "across-the-wire" to the browser or the database.

When passing a HashMap, you are sharing a simple pointer that references the HashMap from your servlet to the JSP page.

There isn't any difference between these two options in terms of security, but in terms performance, it is a few orders of magnitude difference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download