Servlet File Download - java.lang.IllegalStateException: Cannot forward after response has been comm

Discussion in 'Java' started by PythonAnimal@gmail.com, Mar 24, 2006.

  1. Guest

    I keep getting this error using Eclipse and Tomcat. I went over my
    code a 100 times and I do not close the output stream or do I make more
    than one call here is my code.

    I call the method( HttpServletRequest req, HttpServletResponse resp)
    then I do a
    RequestDispatcher _view = req.getRequestDispatcher("/errorPage.jsp") ;
    // return to error
    _view.forward(req,resp);
    and I get the illegalstateExceptionerror

    method( HttpServletRequest req, HttpServletResponse resp)
    {

    try{
    // set approriuate data for returning a file ot type .csv
    resp.setContentType("application/vnd.ms-excel");
    resp.setContentLength((int) _outputFile.length());
    resp.setHeader("Content-Disposition","attachment; filename=\"" +
    _outputFile.getName() + "\"");
    outStream = resp.getOutputStream();
    byte[] buf = new byte[8192];
    FileInputStream inStream = new FileInputStream(_outputFile);
    int sizeRead = 0;
    // write to resp stream
    while((sizeRead = inStream.read(buf, 0 , buf.length)) > 0) {
    outStream.write(buf, 0 , buf.length);
    }

    }catch(Exception e) {System.out.println("ERROR");
    e.printStackTrace();
    return false;
    }

    return true;
    }

    // Here is my forward
    RequestDispatcher _view = req.getRequestDispatcher("/errorPage.jsp") ;
    // return to error
    _view.forward(req,resp);


    I just can not forward to another page after writing, I did this same
    code on a different m achine.
    Thanks so much!! I am dieing over here trying to figure thius forward
    out!!!! using newest SDK.
     
    , Mar 24, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > I just can not forward to another page after writing, I did this same
    > code on a different m achine.


    I'm not familiar with JSP, but this is normal in, for example, PHP. The
    HTTP protocol allows for the server to request that the browser redirects
    itself to a different URL, and this data must be sent in the head of the
    HTTP message, before any content from the body is written.

    If you write content to the body (in PHP, at least), then you may no
    longer send any headers. Perhaps you should rethink the design of your
    application, and delay writing to the body as long as possible, to leave
    yourself the flexibility to write headers?

    - Oliver
     
    Oliver Wong, Mar 24, 2006
    #2
    1. Advertising

  3. R Guest

    Once the response has been writen to the out put stream , you can't
    forward to another page. You may keep the content you are writing in a
    buffer and flush it at the end if there are no errors.
     
    R, Mar 24, 2006
    #3
  4. Re: Servlet File Download - java.lang.IllegalStateException: Cannotforward after response has been committed

    wrote:
    > I keep getting this error using Eclipse and Tomcat. I went over my
    > code a 100 times and I do not close the output stream or do I make more
    > than one call here is my code.
    >

    snip
    > // write to resp stream
    > while((sizeRead = inStream.read(buf, 0 , buf.length)) > 0) {
    > outStream.write(buf, 0 , buf.length);
    >


    Your code would write _outputFile.length() % 8192 more bytes that the
    content length specified.

    It should be:
    while((sizeRead = inStream.read(buf, 0 , buf.length)) > 0) {
    outStream.write(buf, 0 , sizeRead)
    -Misk
     
    Missaka Wijekoon, Mar 25, 2006
    #4
  5. DaBeef Guest

    I changed the code but I still receive the error, i use teh same code
    on a dif tomcat jvm and it works, I am wondering if it is a tomcat
    issue
     
    DaBeef, Mar 27, 2006
    #5
  6. Guest

    DaBeef wrote:
    > I changed the code but I still receive the error, i use teh same code
    > on a dif tomcat jvm and it works, I am wondering if it is a tomcat
    > issue



    Hi there,

    i too get the similar exception when i forward to another jsp from a
    jsp file.


    "java.lang.IllegalStateException: Cannot forward after response has
    been committed
    at
    org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:324)
    at
    org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)"


    my jsp file imports <%@taglib uri="/tags/struts-html" prefix="html" %>
    , is this causing the problem?......as i see some out.println(); in
    generated servlet. any pointers to solve this problem.....?

    Thanks in advance,
    Srivani.
     
    , Mar 28, 2006
    #6
  7. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > DaBeef wrote:
    >> I changed the code but I still receive the error, i use teh same code
    >> on a dif tomcat jvm and it works, I am wondering if it is a tomcat
    >> issue

    >
    >
    > Hi there,
    >
    > i too get the similar exception when i forward to another jsp from a
    > jsp file.
    >
    >
    > "java.lang.IllegalStateException: Cannot forward after response has
    > been committed
    > at
    > org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:324)
    > at
    > org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)"
    >
    >
    > my jsp file imports <%@taglib uri="/tags/struts-html" prefix="html" %>
    > , is this causing the problem?......as i see some out.println(); in
    > generated servlet. any pointers to solve this problem.....?


    Try doing the redirect before any of the println()s occur, or if your
    server enables it, turn on output buffering.

    - Oliver
     
    Oliver Wong, Mar 28, 2006
    #7
  8. Guest

    I am not doing any println, is it have something to do with the init()
    or service() method which I do not have in this servlet, just the
    doPost and another method which does gets a few reqquest paramaters and
    does this
    _outStream = resp.getOutputStream();
    resp.setContentType("application/vnd.ms-excel");
    resp.setContentLength((int) _outputFile.length());
    resp.setHeader("Content-Disposition","attachment; filename=\"" +
    _outputFile.getName() + "\"");
    FileInputStream inStream = new FileInputStream(_outputFile);
    // write to resp stream
    while((sizeRead = inStream.read(buf, 0 , buf.length)) > 0)
    _outStream.write(buf, 0 , sizeRead);
    this._outputFile = null;
    this._outStream = null;

    Other than that I simply go to the request dispatcher for a
    RequestDispatcher _view = req.getRequestDispatcher("/errorPage.jsp") ;
    // return to error
    _view.forward(req,resp);
    return;
    Losing my mind on this one thanks
     
    , Mar 30, 2006
    #8
  9. Oliver Wong Guest

    <> wrote in message
    news:...
    >I am not doing any println, is it have something to do with the init()
    > or service() method which I do not have in this servlet, just the
    > doPost and another method which does gets a few reqquest paramaters and
    > does this
    > _outStream = resp.getOutputStream();
    > resp.setContentType("application/vnd.ms-excel");
    > resp.setContentLength((int) _outputFile.length());
    > resp.setHeader("Content-Disposition","attachment; filename=\"" +
    > _outputFile.getName() + "\"");
    > FileInputStream inStream = new FileInputStream(_outputFile);
    > // write to resp stream
    > while((sizeRead = inStream.read(buf, 0 , buf.length)) > 0)
    > _outStream.write(buf, 0 , sizeRead);
    > this._outputFile = null;
    > this._outStream = null;
    >
    > Other than that I simply go to the request dispatcher for a
    > RequestDispatcher _view = req.getRequestDispatcher("/errorPage.jsp") ;
    > // return to error
    > _view.forward(req,resp);
    > return;
    > Losing my mind on this one thanks


    The line that reads "_outStream.write(buf, 0, sizeRead)" is emitting
    non-header output to the browser. If output buffering is not enabled, then
    you cannot send headers once non-header output has been sent. So you cannot
    send a header which says "Please forward to this page", for example.

    - Oliver
     
    Oliver Wong, Mar 30, 2006
    #9
  10. sameer.rede

    Joined:
    Sep 4, 2007
    Messages:
    1
    Getting Error in downloading file.

    java.lang.IllegalStateException: Cannot forward after response has been committed....

    Can any body please help me out in this problem. I also add my code here for your reference.

    I am trying to write all the Records from the ArrayList in to an Excel File. Which I did already but now I want give the dialog box to the user to Open OR Download that file, and I can not do that. Can any body please tell me...


    My Code --

    HSSFWorkbook workBook = new HSSFWorkbook();
    HSSFSheet outputSheet = workBook.createSheet();
    HSSFCell outputCell = null;
    int count = 0;
    HSSFRow outputHeaderRow = outputSheet.createRow(0);

    for(count=0;count<headerList.size();count++) {
    outputCell = outputHeaderRow.createCell((short)count);
    HSSFRichTextString newHeader = new HSSFRichTextString(headerList.get(count).toString()) ;
    logger.debug("\n\n Name of the Inserted Header -- "+newHeader);
    outputCell.setCellValue(newHeader);
    }

    workBook.write(fp);
    RequestDispatcher dispatcher = request.getRequestDispatcher(finalFileName);
    dispatcher.forward(request, response);
     
    sameer.rede, Sep 4, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page