Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Servlet File Download - java.lang.IllegalStateException: Cannot forward after response has been committed

Reply
Thread Tools

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

 
 
PythonAnimal@gmail.com
Guest
Posts: n/a
 
      03-24-2006
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.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-24-2006
<> wrote in message
news: oups.com...
>
> 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

 
Reply With Quote
 
 
 
 
R
Guest
Posts: n/a
 
      03-24-2006
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.

 
Reply With Quote
 
Missaka Wijekoon
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
DaBeef
Guest
Posts: n/a
 
      03-27-2006
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

 
Reply With Quote
 
srivania12@gmail.com
Guest
Posts: n/a
 
      03-28-2006

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.doF orward(ApplicationDispatcher.java:324)
at
org.apache.catalina.core.ApplicationDispatcher.for ward(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.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-28-2006

<> wrote in message
news: ups.com...
>
> 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.doF orward(ApplicationDispatcher.java:324)
> at
> org.apache.catalina.core.ApplicationDispatcher.for ward(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

 
Reply With Quote
 
PythonAnimal@gmail.com
Guest
Posts: n/a
 
      03-30-2006
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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-30-2006

<> wrote in message
news: oups.com...
>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

 
Reply With Quote
 
sameer.rede sameer.rede is offline
Junior Member
Join Date: Sep 2007
Posts: 1
 
      09-04-2007
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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot get my JSP to forward to database-handler servlet (innetbeans) terry433iid@yahoo.com Java 2 11-06-2009 02:56 AM
Cannot create a session after the response has been committed vincente13@gmail.com Java 0 12-07-2006 09:29 AM
Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response javadev Java 5 11-16-2006 11:22 AM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57