views:

36

answers:

2

Am working on an application which has a requirement to download bulk data from a file server with a maximum file size constraint fixed at 3 Mb.It means i would have a maximum download capacity of 3Mb.I am writing the input stream to the response output stream .My problem is I need to redirect my response to a jsp page after I downlaod.Now when I redirect using the code from my servlet..I get a java.lang.IllegalStateException: Cannot forward after response has been committed

RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/save.jsp");
rd.forward(request, response);

How can I redirect.Will downloading in a separate thread help me .Please advice ....

A: 

You cannot redirect while the response has been already set. Further details here.

thelost
When I complete my download being in the same servlet then why cant i redirect???
Sam
You cannot do that, the servlet already sets some output. However, you could use AJAX push to achieve that, if I'm getting you right.
thelost
+1  A: 

You cannot do it.

Basically, what "committed" means is that the server-side has started sending the response message to the client. At the very least, it has sent the first "line" of the response that contains the response status code. And if you want to do a redirect, that is implemented (in part) by setting a 3xx status code in the response.

You will have to figure out some way to do the redirect before the response gets committed.

Stephen C