When a user clicks the download button, I have a file to be downloaded from the servlet without refreshing the page. How can this functionality be achieved? Below is my JSP code. Users input credentials in the search box to download the file, so the file searching logic is implemented in the servlet.
<form action="Download_Servlet" class="download" method="post">
Search:<input type="text" name="dropdown" id="datedropdown">
<input type="submit" id="downloadRecords" value="Download">
Here is my servlet code:
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=abc.csv");
ServletOutputStream out = res.getOutputStream();
for (Order traverse : orderMap.values())
{
out.write(traverse.toString().getBytes());
out.write("\n".getBytes());
out.flush();
}
The reason for using a loop is to write hundreds of records into the file and then flush it. The issue arises when the user clicks the download button because the page is trying to redirect, even though there is no code in the servlet to handle this redirection. The desired behavior is that when the download button is clicked, the file should be downloaded without the page being redirected.