By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Problem in response.sendRedirect()

P: 19
Hi all,

I have one requirement in my project. I need to download a file from the server tot the client machine. And after downloading it , it should be redirected to another jsp file.

The code for download write inside a servlet is as follows

public class DownloadServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
actionZipDownloadFile(zip_filename, filename,response);


private void actionZipDownloadFile(String zip_filename, String filename,HttpServletResponse response) throws IOException {
System.out.println("I am in actionZipDownloadFile");
System.out.println("The zip_filename=="+zip_filename);
System.out.println("The filename=="+filename);
File f = new File(zip_filename);
InputStream in = null;
try {
jcifs.util.MimeMap mimeMap = new MimeMap();
response.setContentType( mimeMap.getMimeType( "zip" ));
response.setHeader( "Content-Disposition", "attachment; filename=\""+filename+"\"" );
response.setHeader( "Content-Length", Long.toString(f.length()) );
response.setHeader( "Accept-Ranges", "Bytes" );
in = new FileInputStream( f);
OutputStream outStream = response.getOutputStream();
byte[] b2 = new byte[8192];
int n = 0;
while( (n = > 0 ) outStream.write(b2,0,n);
catch (Exception e) {System.out.println("The exception is"+e);} // happens if users interrupts the connection
if (in != null) in.close();
if (f.exists()) f.delete();

File is attached to the response Header in this line
response.setHeader( "Content-Disposition", "attachment; filename=\""+filename+"\"" );

The file is downloaded successfully by means of this code. But that redirect function is not at all called.

I am getting the following exception.

StandardWrapperValve[DownloadServlet]: Servlet.service() for servlet DownloadServlet threw exception
at org.apache.coyote.tomcat5.CoyoteResponseFacade.sen dRedirect(
at DownloadServlet.doPost(
at javax.servlet.http.HttpServlet.service(HttpServlet .java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
at ternalDoFilter(
at Filter(
at org.apache.catalina.core.StandardWrapperValve.invo ke(
at org.apache.catalina.core.StandardValveContext.invo keNext(
at org.apache.catalina.core.StandardPipeline.invoke(S
at org.apache.catalina.core.StandardContextValve.invo keInternal(
at org.apache.catalina.core.StandardContextValve.invo ke(
at org.apache.catalina.core.StandardValveContext.invo keNext(
at org.apache.catalina.core.StandardPipeline.invoke(S
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.core.StandardValveContext.invo keNext(
at org.apache.catalina.valves.ErrorReportValve.invoke (
at org.apache.catalina.core.StandardValveContext.invo keNext(
at org.apache.catalina.core.StandardPipeline.invoke(S
at org.apache.catalina.core.StandardEngineValve.invok e(
at org.apache.catalina.core.StandardValveContext.invo keNext(
at org.apache.catalina.core.StandardPipeline.invoke(S
at org.apache.catalina.core.ContainerBase.invoke(Cont
at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co
at org.apache.coyote.http11.Http11Processor.process(H
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:549)
at org.apache.tomcat.util.threads.ThreadPool$ControlR
at Source)

I am able to guess that the response is already committed after that file is downloaded to the client.. But my requirement is I have to forward to another page after the file is downloaded.

Can u guide me how to do this……
Aug 2 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.