JSON in JAVA
Check out latest Mobile Phones and Books only at Flipkart.com
JSON is being widely used in Web Technology for data transfer in JavaScript. But with AJAX coming into picture JSON has become the most popular tool for sending data from remote page to the calling page. Well there are different packages available for implementing JSON in Java. Here i am using one of the package to show how we can use the JSON package in Java.
The JSON Package that i will be using is from: JAVA JSON Package
This article will give a small glimpse of how we can send large data from JSP/Servlet to client Page using JSON and AJAX.
The JSON Package used here can send data in two different format JSONArray and JSONObject. Tha main difference between JSONArray and JSONObject is that JSONArray uses ArrayList to store the data whereas JSONObject uses HashMap to store the data.
Java Servlet implementing JSON
import org.json.simple.JSONObject; public class json extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{ JSONObject array= new JSONObject(); array.put("data1","Hello"); array.put("data2","World"); array.put("data3","Good"); array.put("data4","Morning"); } public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{ doPost(); } }
HTML Page reading JSON value:
<html> <head> <script language="Javascript"> function getHTTPObject() { var xmlhttp = false; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } } return xmlhttp; } var xmlhttp = new getHTTPObject(); //Initialize New Ajax Object function Connect2RemoteSite(url,funcname,parameter) { try { if(xmlhttp) { xmlhttp.open("POST", url, true); //URL will be the servlet page xmlhttp.onreadystatechange = funcname; xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.setRequestHeader("Content-Length", parameter.length); xmlhttp.send(parameter); } else { document.getElementById("rotateimage").style.display = "none"; alert("Your Browser dosen't support Ajax"); } } catch(e) { alert("Some Unknown Error Occured. Please Try Again" + e); } } function AjaxResponse() { if(xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { //servletdetail holds the JSONArray Details servletdetail = eval(xmlhttp.responseText); } } } </script> </head> <body> </body> </html>
Here i have given the JSON implementation with AJAX. JSON object is getting created in the Servlet and is getting passed to the javascript through Ajax. Now in JavaScript the AjaxResponse Function reads the JSON object by calling eval function. eval function is inbuild function of javascript and here it is responsible for reading JSON Object data and will assign the data to javascript variable
Related Articles:
- Reading New Emails from Java Applications
- UTF-8 Encoding Email Content using Java
- Publish on Facebook WALL using Java
- Singleton Design Pattern in Java
- Log4J Logging Inside Eclipse Console
- HTTP POST File Content in JAVA
- Uninstall Java Development Kit on Linux Systems
- Performing Text To Speech (TTS) conversion on linux using Java
- Modifying / Editing XML Document in JAVA
- Reading POP3 Mails Using Java
i created one json file with some pair of values and i displayed those values in button click of html page.
its working properly in fire fox(i.e json values get displayed in page using firefox)but not displaying values in page while using internet explorer.what is reason iam not getting
Hello hitesh,
can you say me , i have successfully compiled this servlet file. but when i am running this html(jsoncode). its not showing that data..only blan screen?
Hi Sohil,
You will have to set content type as JSON or need to perform out.println(JSON String) to get the data.
Hope this helps.
Thanks,
Hitesh
Hello.
Thank you very much for your example. I have a few questions. I’am new to all this and I don’t know how to use this code. What parts do I have to change if any (url, funcname, parameter maybe)? Can I use portlet instead of html page and put servlet nad portlet in same project in eclipse? Thank you very very much.