0

I have a project with servlets in the back and JS in the front.

When I run servlet by pasting address in browser it works fine. But when I want to run it with button in JS, nothing happens.

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ExcelParser ep = new ExcelParser();
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheetl");
    response.setHeader("Content-Disposition", "attachment; filename=workbook.xlsx");

    XSSFWorkbook workbook = ep.getWorkbook();
    ServletOutputStream out = response.getOutputStream();
    workbook.write(out);
    out.flush();
    out.close();
}

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

JS

Template.exportButton.events({
    "click #exportButton": function (event) {
        document.getElementById("exportButton").innerHTML = "Started";
        $.get("https://mypage/pu/rec/build/positions/excel");
        document.getElementById("exportButton").innerHTML = "Finished";
    }
});

But when I click on Export Button on page, it changes text to "Finished", but servlet is not executed.

I have another servlet that look similar to above one and JS button with

$.get("https://mypage/pu/rec/build/positions/back?email=" + email, function (data) {});

and it works. How can I run this servlet with JS?

EDIT: Problem must be with servlet. I've changed the address in exportButton to second one and it works (the "back" servlet runs sh scripts on server). Is it because I try to send excel sheet in my servlet? If so, how can I send generated file to client?

korek
  • 113
  • 11
  • Could the `$.get()` be running asychronously, meaning the button is changed before the request is actually completed? You could change the button in the `success` callback once the request comes back to make the timing make sense. Or is the server not getting anything at all? – Whothehellisthat Jul 21 '16 at 11:12
  • First thing I would do with your code is adding few `log.debug()` or `System.out.println()` calls - at least at the start and end of the `doGet()` method. Then you can see in the log, whether the servlet was called or not. And of course, @Whothehellisthat is right as well - `$.get()` is executed asynchronously, i.e. `exportButton` can be set to "Finished" **before** the AJAX call is actually executed. – Jozef Chocholacek Jul 21 '16 at 11:59
  • I've added some system.outs. In browser it works great (no surprise here), but through JS button, doGet() method is not even called. – korek Jul 21 '16 at 12:27
  • Have you debugged the Javascript from browser's console? Is there a network call (i.e. HTTP request) fired when `$.get()` line is executed? What is the response when using the not-working URL (the `.../excel` one) and what is the response with the `.../back?...` one? Btw. perhaps the problem is in the servlet mapping, can you share your `web.xml`? – Jozef Chocholacek Jul 21 '16 at 13:07
  • In Network tab in browser console (Opera), when I click the button, a line "Name : excel, Status : 200, Type : xhr" appears with details in response headers "Content-Disposition:attachment; filename=workbook.xlsx Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheetl". So it looks like it connected but does not dowloaded file. When I', trying to access servlet via browser it is again "Name : excel, Status : 200" but type is document – korek Jul 21 '16 at 13:44
  • Well, most probably it does download the file, but this is not the way to download and save the file. The file is only available for the Javascript to process. Luckily, there are some workarounds, read e.g. http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Jozef Chocholacek Jul 21 '16 at 13:56

0 Answers0