title: Downloading file from Servlet called via JS POST
body: <p>I am using JS to obtain a variable and then make a POST call to a Servlet. I am not using Ajax explicitly.</p>
  
  <pre><code>$('#download').click(function (e) {
  ....
          $.post('downloadServlet', {'var': variable});
          //e.preventDefault();
  });
  </code></pre>
  
  <p>The Servlet has code to read a file and, in theory, send it, but I do not get any response.</p>
  
  <pre><code>   @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  ....
          response.setContentType("application/octet-stream");
          response.setContentLength(...);
          response.setHeader( "Content-Disposition", String.format("attachment; filename=%s", path.getFileName()));
          try (OutputStream out = response.getOutputStream()) {
              Files.copy(path, out);
              out.flush();
          }
          System.out.println("Done");
  </code></pre>
  
  <p>My questions is Why is the file not sent/received? From a technical point, I am not sure I understand why it doesn't work, since I am not using Ajax. The POST method seems to execute as any normal submit form. I have read <a href="https://stackoverflow.com/questions/7563791/is-it-possible-to-download-a-file-with-http-post">this</a> but doesn't clarify. Any didactic explanation will be appreciated.</p>