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

P

PythonAnimal

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.
 
O

Oliver Wong

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
 
R

R

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.
 
M

Missaka Wijekoon

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
 
D

DaBeef

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
 
S

srivania12

DaBeef said:
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.
 
O

Oliver Wong

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
 
P

PythonAnimal

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
 
O

Oliver Wong

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
 
Joined
Sep 4, 2007
Messages
1
Reaction score
0
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top