How to call response.sendRedirect() method ,after the response is already committed?

Posted on 2006-05-23
Last Modified: 2012-08-13
I have generated the file download dialog box.If i click the Open or Save or Cancel  on the dialog box,then appropriate actions have been happening.After that i have to redirect to another page.The problem is that once the response is already committed, then i am unable to redirect to another page.How to resolve this problem.

               This is the code.

<%@ page import="*"%>

                InputStream in1 = null;
      OutputStream out1 = null;
            in1 = getServletContext().getResourceAsStream("/doc/emphandbook.pdf");
            out1 = new BufferedOutputStream( response.getOutputStream() );
            in1 = new BufferedInputStream( in1 );
            response.setHeader("Content-Disposition","attachment; filename=\"emphandbook.pdf\"");
            int c;

      catch(Exception e)
            System.out.println("Errot Message1 ::: "+e);

Question by:Vasanth_Kumar
    LVL 5

    Expert Comment

    have you tried reversing the order of:

    out1.close();     response.sendRedirect("");

    LVL 3

    Accepted Solution

    This is actually something that a lot of people fall on, I find.

    What you are trying to do is not supported. You cannot sound output to the screen (by writing to out) and then re-direct the client. The reason for this is that doing a response.sendRedirect changes the HTTP headers that are sent to the client, and you can't do this twice.

    Unfortunatly you will need to work out a better solution. It appears what you want to do is have the user download a file, and then be re-directed to a site. I believe one way a lot of people do this is to have a link open a new window which downloads the file, and closes itself, and at the same time, navigates the user in the main window to where they want them.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Fisheye tool 2 97
    gZip compression filter 2 76
    best tutorial on log4j and logging for servlets/jsp codes 1 61
    love6 challenge java 31 126
    It Is not possible to enable LLDP in vSwitch(at least is not supported by VMware), so in this article we will enable this, and also go trough how to enabled CDP and how to get this information in vSwitches and also in vDS.
    OfficeMate Freezes on login or does not load after login credentials are input.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now