1

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();

  }); 
jffryteo
  • 31
  • 4
  • That `JSON#parse()` call makes no utter sense here. Your string is in JSON format already. Hint for future problems with JavaScript: press F12 in your webbrowser and open the *Console* to see all errors/warnings. And, perform a *View page source* to inspect if the JSP-produced HTML output contains the proper JavaScript syntax. – BalusC Feb 20 '20 at 07:41
  • Hi Balus, i came across that thread before, correct me if i'm wrong, so i already have acquired the JSON string "listJ" from my request parameter. I'm trying to print that JSON string data into a table. How do i access that data, that's my other issue. I've been using console to do it as well but i'm not sure what i'm looking for too. Usually my console will show that the var a = *smth empty* does this mean, it cannot detech anything? – jffryteo Feb 21 '20 at 01:24
  • The duplicate tells you to use the `` syntax. In your case that would thus be `var listJson = ${listS};` – BalusC Feb 21 '20 at 05:37

0 Answers0