I am using JS to obtain a variable and then make a POST call to a Servlet. I am not using Ajax explicitly.
$('#download').click(function (e) {
....
$.post('downloadServlet', {'var': variable});
//e.preventDefault();
});
The Servlet has code to read a file and, in theory, send it, but I do not get any response.
@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");
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 this but doesn't clarify. Any didactic explanation will be appreciated.