Question

I have a download.jsp page that when loaded, causes a file to download using the following code:

String contentDisp = "attachment; filename=file_" + "."+DeptNumber+ ".txt";     
response.setContentType("text/plain");
response.setHeader("Content-Disposition", contentDisp);

After this, i do some out.write(....) statements and then in the end out.flush() which is when the user receives a download file request from the browser. After that i use:

response.sendRedirect("landingpage.jsp");

To move the user on to the next page.

Now, I want to do the same but instead of one file, i want the page to cause 2 files to be generated for download. I did 2 changes:

1) I changed the beginning of the jsp to check for an attribute, and according to the attribute sent, generate the correct file for download.

String downloadDeptNumber = request.getAttribute("dept")==null ? "1" : request.getAttribute("dept").toString();

and

2) In the end of the page instead of redirecting to the next page, i check if the attribute was 1, i change it to 2 and redirect to the same page. If it wasn't 1, i redirect to the end page (landing page) that i used originally with only 1 download above.

if (downloadDeptNumber.equals("1")) 
{
      redirectUrl="download.jsp" ;
      session.setAttribute("dept", "2");
}
else
{
      redirectUrl= "landingpage.jsp";
      session.removeAttribute("dept");
}
response.sendRedirect(redirectUrl);

But it seems i can't redirect to the same page i am on currently.

Does anyone have a solution for me ?

Thanks.

Was it helpful?

Solution

option # 1 : separate link

Give 2 separate link to download each file

option #2 : zip file

make a single zip file on server using java API and download

option #3 : Use java script

make a java script function that will open the new tab and download the each file simultaneously

Below code should run to download each file

window.open(
  'http://download_file_link',
  '_blank' // <- This is what makes it open in a new window.
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top