i have a servlet where it fetches my data from MySQL, i forward the request to my JSP page, at the moment, i can't seem to fetch my data properly.
I can get my JSON String "listJ" just fine, but i am unable to assign it to a var "listJson". Can anyone advise me on the right way to fetch data, i will eventually need to implement AJAX and i think the only way forward is that the data has to be in JSON form in order to do that.
My previous issue is: Using Jquery Ajax on JSP to display on Datatables
Servlet
ArrayList<s> listS = DBUtils.load(searchKey);
....
String json = gson.toJson(listS);
request.setAttribute("listS", json);
request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, response);
JSP
<script>
$(function () {
<% String listJ = (String) request.getAttribute("listS"); %>
var listJson = JSON.parse(<%=listJ%>);
var table = $("#s").DataTable({
"scrollY": 500,
"scrollX": true,
"paging": true,
"lengthChange": false,
"searching": true,
"ordering": true,
"info": true,
"autoWidth": false,
"fixedHeader": true,
"deferRender": true,
"columns": [ // map the columns here
{ "data": "a" },
{ "data": "b" },
]
});
//render list here
table.clear();
table.rows.add(listJson); // make sure that list should be json object and not text
table.draw();
});