Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82602

Simple pass of JSON to Datatable via response.setAttribute()

$
0
0

Hey all,

I have successfully constructed a JSON string and have it prepared to be passed to my .jsp file.The issue is, I cannot get the .jsp file with the data table to load. The file will open when I run the stand alone .jsp file, or when I do a request.getRequestDispatcher("/resources/runners/runnerHistory.jsp"); call. But not when I am trying to pass JSON with Ajax or .setAttribute().

I am at a loss of how to get this page, and table to load with a JSON string.

This is the .jsp I am trying to load...

$(document).ready(function() {
            var table;
            //Pretty much the main method of javascript (with jquery)
            $(function() {////////////////////main function////////////////////////////
                //Table was given the id table_users
                //Create datatable from table
                table = $('#table_runners').DataTable(
                {
                 //Url is from table_user class
                "ajax" : "${pageContext.request.contextPath}/runnerhistorycontroller",
                //Setting columns to get specific data from json response
                "columns" : [
                    {
                        data : null,
                        className : "center",
                        defaultContent :
                            '<button onclick= "editFunction();" id="btn_editusermodal" class = "btn btn-primary" >edit</button><button onclick=" " id="btn_deleteusermodal" class = "btn btn-primary">delete</button><button onclick=" " id="btn_runnerhistorymodal" class = "btn btn-primary">history</button>'
                        },
                        {
                          "data" : "runnerID" , "defaultContent": "<i>Not set</i>"},

                          {"data" : "runnerFName", "defaultContent": "<i>Not set</i>"},
                        {"data" : "runnerLName", "defaultContent": "<i>Not set</i>"},
                        //ensure that fields match
                        //from column mapping in servlet
                        {"data" : "raceName", "defaultContent": "<i>Not set</i>"},
                        {"data" : "raceTime", "defaultContent": "<i>Not set</i>"}
                        ],
                    }

This is the servlet that pulls the query and constructs JSON

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String json = request.getParameter("rid");
        //String ridIn = (String)request.getAttribute("rid");

         String runnerID = "{\"runnerID\":"+json+"}";
        System.out.println(runnerID + " rid as json string new");


        //String json = request.getParameter("rid");
        RunnerHistory r = null;
         Gson gson = new Gson();
         r = gson.fromJson(runnerID, RunnerHistory.class);
        //System.out.println(r + " test JSON in ");


        List<RunnerHistory> runners;
        CallableStatement callStmt = null;
        ResultSet myRS = null;


        try (Connection conn = ConnectionManager.getConnection()) {
              String  ridParam  =  r.getRunnerID();
            runners = new ArrayList<RunnerHistory>();
            callStmt = conn.prepareCall("{call get_runnerhistory(?)}");
            callStmt.setString(1, ridParam);
            callStmt.execute();
            myRS = callStmt.executeQuery();

            while (myRS.next()) {

                String  rid = myRS.getString("runnerID");
                String rfn = myRS.getString("runnerFName");
                // runners.add(runnerFName);
                String rln = myRS.getString("runnerLName");
                // runners.add(runnerFName + " " + runnerLName);
                String rn = myRS.getString("raceName");
                // runners.add(runnerAge);
                String rt = myRS.getString("raceTime");

                // runners.add(runnerClass);
                r = new RunnerHistory(rid, rfn, rln, rn, rt);
                runners.add(r);
            }

            /*for(RunnerHistory i: runners){
                System.out.println(i + "list2");
            }*/
            // Gson gsonOut = new Gson();
             // String jsonObject=new Gson().toJson(r);
             // System.out.println(jsonObject + " <---JSON");

               Gson gsonOut = new Gson();
               DataTable table = new DataTable();
               table.setData(runners);
               String jsonObject = gsonOut.toJson(table);
              System.out.println(jsonObject + " <---JSON");

               //request.setAttribute("json",jsonObject);
              //RequestDispatcher rd = request.getRequestDispatcher("/json/table/getrunnerhistory");
                //RequestDispatcher rd = request.getRequestDispatcher("/resources/runners/runnerHistory.jsp");

                //rd.forward(request,response);
              System.out.println("request done");

               request.setAttribute("runIDout",jsonObject);
                RequestDispatcher rd = request.getRequestDispatcher("/runnerhistorycontroller");
                rd.forward(request,response);


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

This is another servlet that controls the call to and passes the constructed JSON to the .jsp holding the table. Even if I get rid of the .getRequestDispatcher and rely on the ajax call the .jsp file from above still willl not load!

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String convertJSON = (String)request.getAttribute("runIDout");
        System.out.println(convertJSON + "in controller");
         //String json = "{\"runnerID\":"+convertJSON+"}";
          Gson gsonOut = new Gson();
           DataTable table = new DataTable();
           table.setData(convertJSON);
           String json = gsonOut.toJson(table);

          request.setAttribute("rid",convertJSON);
        RequestDispatcher rd = request.getRequestDispatcher("/resources/runners/runnerHistory.jsp");
         rd.forward(request,response);
         PrintWriter out = response.getWriter();
         out.print(json);
         out.flush();
    }


Viewing all articles
Browse latest Browse all 82602

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>