How to forwrd to another server/jsp page when file downloading finished in one servlet page.

I have a file download servlet that allow user to download a file from ftp.
when download finished, i need to forwared to other page (jsp or servlet). but when i do it, error :

IllegalStateException : 'Cannot forward after response has been committed '

=============source code =========================
package downloadservlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0

public class downloadServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  public void init() throws ServletException {
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    response.setContentType(CONTENT_TYPE);
//    PrintWriter out = response.getWriter();
//    out.println("<html>");
//    out.println("<head><title>downloadServlet</title></head>");
//    out.println("<body bgcolor=\"#ffffff\">");
//    out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
//    out.println("</body></html>");
    OutputStream out=response.getOutputStream();
                       "attachment; filename=example.pdf");
    String fileURL="";
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
      URL url = new URL(fileURL);

      bis = new BufferedInputStream(url.openStream());
      bos = new BufferedOutputStream(out);

      byte[] buff = new byte[2048];
      int bytesRead;

      while ( -1 != (bytesRead =, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);

      System.out.println("Download completed");
      ///////ERROR OCCURED/////////////
      (request.getRequestDispatcher("aaa.jsp")).forward(request,response);   ///
    catch (final MalformedURLException e) {
      throw e;
    catch (final IOException e){
      throw e;
    finally {
      if (bis != null)
      if( bos != null)

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  //Clean up resources
  public void destroy() {

Is there have any way i can forware to other page after download completed ?
Who is Participating?
Not with doing it this way...  the problem is that you cannot send a forward or a redirect AFTER you have sent some data...  

That's not part of the HTML spec, so can't be done :-(


Does this work:

    RequestDispatcher disp;
    disp = request.getRequestDispatcher( "aaa.jsp" ) ;
    disp.include( request, response ) ;

it might do (it is basically a server side include),

But it might not :-/

Good luck

Yes this is known and normal, on the response u can't change any attribute of the response and then forward,

I see u forward to the aaa jsp after doing set content type, header and sending contents

I think the line:

     (request.getRequestDispatcher("aaa.jsp")).forward(request,response);   ///

mean't to be in somewhere else
If u want to forward to JSP after finishing download use sendRedirect
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

>  If u want to forward to JSP after finishing download use sendRedirect

But sendRedirect doesn't work after a response has been started or committed either...

So that would give the same error...
From the API docs for "HttpServletResponse.sendRedirect()":


    If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
right TimYates,

Anyway i never saw a web application forwards a user to a page after downloading something, it is usually the opposite
True...  Usually you are left on the download page (as it makes it easier to re-start the download if it stops, or has errors)

I wonder if the "include" method works though...  

All of my gut tells me no...  

But a small part of my brain says "maybe" in a very quiet voice

you can use javascript:-
first link to your JSP page you want to forward to.
add some code in your jsp page as such:-
<script language="javascript">
function downloadPDF() {
   document.location.href="<path to servlet>";
<body  onload="downloadPDF();">
get rid of the forward in your servlet
i usually set response.setContentType("application/octet-stream"); , not "application/download" but it might work as well.

this will have the effect you're looking for, i think. The pdf will download and ask the user to save without giong to another page.
I think the idea is to go to a different page once the download is complete...
The answer to this question is "you can't"

so I reckon a split between TimYates and petmagdy
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.