This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Download Servlet not forwarding control  RSS feed

Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am kinda writing an Application that allows users to upload , checkin and Check out files. I am done with Upload Code.for the Checkout code i wish to allow the user to be able to download files. i am using the following JSP

<%@page import="java.util.*,user.UserFiles,*,org.apache.commons.fileupload.*;"%>

UserFiles userFile = (UserFiles)request.getAttribute("CheckoutFile");
response.addHeader("Content-Disposition", "attachment; filename="+userFile.getFileName());
FileItem checkoutItem = userFile.getFile();
InputStream fis = checkoutItem.getInputStream();
PrintWriter pw = response.getWriter();
int c=-1;
// Loop to read and write bytes.
while ((c = != -1)
// Close output and input resources.
}catch (Exception er)

<h4>Display JSP</h4>
<h4><% out.println("Download Successful"); %><h4>
<form action="UserTask.jsp">
<input type="Submit" value="click here to go back">

And i am calling this JSP from the Servlet...

RequestDispatcher d_dispatcher = p_request.getRequestDispatcher("Display.jsp");

The problem i am facing out here is that servlet simply doesnt pass the conrtol to the JSP page. Rather . as i execute the servlet , a window pops up allowing me to donwload the file from the Server but the control remains with the Servlet. it doesnt get passed to the JSP page. please can anyone suggest whats going wrong? Thanking in advance,

Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only return one response per request.
One way of dealing with this is to, first, return the JSP page you want to render.
Have that page make the request for the file to be downloaded. You can do this with either a META refresh tag or with Javascript.
Rohit Malhotra
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am sorry. But can you explain in more Details. or give me an idea about the code?
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!