• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Download Servlet not forwarding control

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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,java.io.*,org.apache.commons.fileupload.*;"%>
<html>


<body>
<%
try
{
UserFiles userFile = (UserFiles)request.getAttribute("CheckoutFile");
response.setContentType("application/unknown");
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.
//pw.print("Test");
while ((c = fis.read()) != -1)
{
pw.print((char)c);
}
// Close output and input resources.
fis.close();
pw.flush();
pw=null;
}catch (Exception er)
{
er.printStackTrace();
}
%>

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

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


{
p_request.setAttribute("CheckoutFile",l_checkfile);
RequestDispatcher d_dispatcher = p_request.getRequestDispatcher("Display.jsp");
d_dispatcher.forward(p_request,p_response);
}


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,

Regards,
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am sorry. But can you explain in more Details. or give me an idea about the code?
 
Oh the stink of it! Smell my tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic