Send messages from Servlet to Java Script

0

I have a question that has taken a little time from me.

I am working with a Handsondtable , component of the JS table. It turns out that when I insert data into the table, I send it to the Servlet by means of a call from the JavaScript. In the servlet I perform all the necessary validations but I have problems when receiving messages in the JSP, since the call was made from the JavaScript and I do not know how to communicate with the Servlet from JS.

Next the code of the called

jQuery.ajax({
    url: "req.jsonServlet", //LLAMO AL SERVLET
    dataType: 'json',
    type: 'POST',  //POR EL METODO POST
    data: {action: "import_items_products_IPD", //EL NOMBRE DE LA ACCION EN EL SERVLET
       datos: quitar(algo),                                                                             
       manufacturer: manufacturerId, 
       contactManufacturer : contactManufacturerId,
       productLine: productLineId,
       harmonizedC: harmonizedCode,
       client: clientId,
       contactClient: contactClientId,
       supplier : supplierId,
       contactSupplier: contactSupplierId},
             success: function () {
                 alert('Products import successfully.');
             },
             error : function(obj,req){
                 alert('Error trying to import the products.');
             }
});

As you can see in the previous code, that's where I make the call to the Servlet from the Script java, but after doing the procedure in the Servlet, I do not know how to return the message and show it with an Alert or what be.

The Servlet code is this, and from there I need to send the messages.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher vista = request.getRequestDispatcher("http://localhost:8080/SOFTLOT/faces/jsp/reports/IPDepartment/importQuoteRequest.jsp");
    try {

        if("import_items_products_IPD".equalsIgnoreCase(request.getParameter("action"))){
            int manufacturerId = Integer.parseInt(request.getParameter("manufacturer"));
            int contactManufacturerId = Integer.parseInt(request.getParameter("contactManufacturer"));
            int productLineId = Integer.parseInt(request.getParameter("productLine"));
            String harmonizedCode = request.getParameter("harmonizedC");
            int clientId = Integer.parseInt(request.getParameter("client"));
            int contactClientId = Integer.parseInt(request.getParameter("contactClient"));
            int supplierId = Integer.parseInt(request.getParameter("supplier"));
            int contactSupplierId = Integer.parseInt(request.getParameter("contactSupplier"));

            String losParametros = request.getParameter("datos");

            ObjectMapper mapper = new ObjectMapper();
            ProductImport [] listadeProductos = mapper.readValue(losParametros, ProductImport[].class);
            List<ProductImport> productsList = Arrays.asList(listadeProductos);


            boolean saved = saveImportedProducts(manufacturerId, contactManufacturerId,productLineId, harmonizedCode, clientId, contactClientId,
                     supplierId, contactSupplierId, productsList);

            if(!saved){
                vista.forward(request, response);
            }

        }

    } catch (Exception e) {
        log.error("error: " + e);
    }
}

Simply what I need is to validate a data from the aforementioned table and return a message from the Servlet to the JavaScript and display it in the JSP.

I thank you in advance for your help, community.

    
asked by Camilo Gutiérrez Castrillon 05.07.2018 в 15:32
source

0 answers