0

I have an editable DataTables (jQuery) that shows the data from my DB in my jsp. I try with no success to retrieve in a servlet the list of objects updated (after modification of the data by a user in the client side) wich are in this DataTables... A "save" button is here to trigg the process.

Here is part of my jsp :

        <button onclick="saveData()">SAVE</button>

        <div class="x_content">
             <table id="myTable" class="table table-striped table-bordered"    width="100%"></table>
        </div>

             <script>
            function saveData() {

                $.ajax({
                    type: 'POST',
                    url: 'AddLicenceServlet',
                    data: 'data',
                    headers: {
                        Accept: "application/json; charset=utf-8",
                        "Content-Type": "application/json; charset=utf-8"
                    },
                    success: function () {
                        var table = $('#myTable').DataTable();
                        var data = table.rows().data();
                        request.setAttribute("data", data);
                        console.log(data);
                        alert('success insert' + data);
                    }
                });
            }
            ;



        <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: 'GET',
                url: 'http://localhost:8080//RedlogServer3/licences',
                dataType: 'json',
                headers: {
                    Accept: "application/json; charset=utf-8",
                    "Content-Type": "application/json; charset=utf-8"
                },
                success: function (response) {
                    var dataTablesObj = response;
                    generateDataTable(dataTablesObj);
                    dataSet = dataTablesObj.data;
                },
                error: function (resultat, statut, erreur) {
                },
                complete: function (resultat, statut) {
                }
            })
        });
        </script>

The console.log(data) in the "success" shows well the updated data that I would like to retrieve in my servlet and the call of the servlet is working but I don't retrieve any data... The "request.getParameter" are "null"...

Here is my servlet :

     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse  response)
        throws ServletException, IOException {
    processRequest(request, response);

    LicenceDAO licenceDAO = new LicenceDAO();
    List<Licence> licenceList = (List)request.getAttribute("data");

    for (Licence c : licenceList) {

    c.setSerialNumber(request.getParameter("pk.serialNumber"));
    c.setSoftware(request.getParameter("pk.software"));
    c.setCustomer(request.getParameter("customer"));
    c.setName(request.getParameter("name"));

    licenceDAO.persist(c);
    }
 }

How can I retrieve the data from the table in the client side to my servlet in the server side using Ajax ? Did I miss something to make the communication between jsp and servlet possible ?

I'm new in servlet/jsp/ajax, any help would be great! Thanks a lot!

GuiDev87
  • 9
  • 2
  • You are not passing your data to the servlet. Document.location.href = "AddLicenceServlet" will just load the servlet. Google for 'datatable post data' – ramp Aug 21 '17 at 11:38
  • ("data": 'data',) should be ("data": 'data') no comma at the end. What do you see in the network tab of inspect when sending the details? It will display what you are sending to the servlet. Helps to debugg – Jonathan Laliberte Aug 22 '17 at 14:09
  • I delete the coma but it did not fix the problem. On the Network it says there's no data available. – GuiDev87 Aug 23 '17 at 08:44
  • I edit the code... I think it's going better but still not working :( – GuiDev87 Aug 23 '17 at 10:56

0 Answers0