• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

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

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="java.io.*"%>

<%
                InputStream in1 = null;
      OutputStream out1 = null;
      try
      {
            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;
            while((c=in1.read())!=-1)
                  out1.write(c);
            in1.close();
            out1.close();
            response.sendRedirect("http://www.realsoftinc.com:8080/rsimainsite/doteco");            

      }
      catch(Exception e)
      {
            System.out.println("Errot Message1 ::: "+e);
      }
%>

0
Vasanth_Kumar
Asked:
Vasanth_Kumar
1 Solution
 
jpolin1Commented:
have you tried reversing the order of:

out1.close();     response.sendRedirect("http://www.realsoftinc.com:8080/rsimainsite/doteco");

?
0
 
KruleCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now