Ajax Json Java Servlet Code - Not Working
Ajax Json Java Servlet Code - Not Working
When i try to view the html page , which invokes the servlet through ajax call, it does not display the data. When URL is viewed the IE, it displays the output. The output is
{"iTotalDisplayRecords":3,"iTotalRecords":3,"aaData":[["1","01\/01\/2013","$0.20","four","five","six","seven"],["2","02\/01\/2013","$0.20","four","five","six","seven"],["3","03\/01\/2013","$0.20","four","five","six","seven"]]}
Url - http://testmachine:8081/jdbs/datatablesTest
This is the html code that was created.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
DataTables example
@import "http://datatables.net/release-datatables/media/css/demo_page.css";
@import "http://datatables.net/media/css/header.ccss";
@import "http://datatables.net/release-datatables/media/css/demo_table.css";
@import "http://datatables.net/release-datatables/examples/examples_support/syntax/css/shCore.css";
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://testmachine:8081/jdbs/datatablesTest",
"fnServerData": function( sUrl, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"url": sUrl,
"data": aoData,
"success": fnCallback,
"dataType": "jsonp",
"cache": false
} );
}
} );
} );
Column 1
Column 2
Column 3
Column 4
Column 5
Column 6
Column 7
Here is the java servlet code
package com.loads.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class datatablesTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {};
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
JSONObject result = new JSONObject();
JSONArray array = new JSONArray();
JSONArray ja = new JSONArray();
ja.add("1");
ja.add("01/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
ja = new JSONArray();
ja.add("2");
ja.add("02/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
ja = new JSONArray();
ja.add("3");
ja.add("03/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
PrintWriter out = resp.getWriter();
int total =3;
int totalAfterFilter=3;
result.put("iTotalRecords", total);
result.put("iTotalDisplayRecords", totalAfterFilter);
result.put("aaData", array);
resp.setContentType("text/jsonp");
/*resp.setHeader("Cache-Control", "no-store");*/
out.print(result);
out.flush();
out.close();
}
}
{"iTotalDisplayRecords":3,"iTotalRecords":3,"aaData":[["1","01\/01\/2013","$0.20","four","five","six","seven"],["2","02\/01\/2013","$0.20","four","five","six","seven"],["3","03\/01\/2013","$0.20","four","five","six","seven"]]}
Url - http://testmachine:8081/jdbs/datatablesTest
This is the html code that was created.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
DataTables example
@import "http://datatables.net/release-datatables/media/css/demo_page.css";
@import "http://datatables.net/media/css/header.ccss";
@import "http://datatables.net/release-datatables/media/css/demo_table.css";
@import "http://datatables.net/release-datatables/examples/examples_support/syntax/css/shCore.css";
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://testmachine:8081/jdbs/datatablesTest",
"fnServerData": function( sUrl, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"url": sUrl,
"data": aoData,
"success": fnCallback,
"dataType": "jsonp",
"cache": false
} );
}
} );
} );
Column 1
Column 2
Column 3
Column 4
Column 5
Column 6
Column 7
Here is the java servlet code
package com.loads.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class datatablesTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {};
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
JSONObject result = new JSONObject();
JSONArray array = new JSONArray();
JSONArray ja = new JSONArray();
ja.add("1");
ja.add("01/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
ja = new JSONArray();
ja.add("2");
ja.add("02/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
ja = new JSONArray();
ja.add("3");
ja.add("03/01/2013");
ja.add("$0.20");
ja.add("four");
ja.add("five");
ja.add("six");
ja.add("seven");
array.add(ja);
PrintWriter out = resp.getWriter();
int total =3;
int totalAfterFilter=3;
result.put("iTotalRecords", total);
result.put("iTotalDisplayRecords", totalAfterFilter);
result.put("aaData", array);
resp.setContentType("text/jsonp");
/*resp.setHeader("Cache-Control", "no-store");*/
out.print(result);
out.flush();
out.close();
}
}
This discussion has been closed.
Replies
Allan