JSON in JAVA
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 Post
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.





















Comments
No comments yet.
Leave a comment