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);
response.sendRedirect(“../../ProdProcess.jsp”);
}

}

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 = in.read(b2)) > 0 ) outStream.write(b2,0,n);
outStream.close();
}
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();
return;
}

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
java.lang.IllegalStateException
at org.apache.coyote.tomcat5.CoyoteResponseFacade.sen dRedirect(CoyoteResponseFacade.java:379)
at DownloadServlet.doPost(DownloadServlet.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:563)
at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:563)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:563)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:563)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:209)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:589)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:666)
at java.lang.Thread.run(Unknown 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.