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.
Oliver Wong - 24 Mar 2006 22:31 GMT
> 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 - 25 Mar 2006 00:41 GMT
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.
Missaka Wijekoon - 25 Mar 2006 04:12 GMT
> 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
DaBeef - 27 Mar 2006 21:31 GMT
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
srivania12@gmail.com - 28 Mar 2006 06:45 GMT
> 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.
Oliver Wong - 28 Mar 2006 16:03 GMT
>> 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
[quoted text clipped - 15 lines]
> , 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
PythonAnimal@gmail.com - 30 Mar 2006 17:53 GMT
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
Oliver Wong - 30 Mar 2006 18:15 GMT
>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
[quoted text clipped - 18 lines]
> 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