File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.IllegalStateException

 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I did lot of changes by implementing many ways by searching this error on google but still exists in my code. Please help me to fix this problem


My code is as follow:


Thanks & best regards
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call sendRedirect if you have written to the output stream, or in your case the Writer.
It is bad practice to put HTML in a servlet, and if you gather the data ready to display and pass to a JSP then your code becomes:

You can call one or the other, but not both.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking it a step further, the HTTP response that you send back to the client is made up of a HTTP header and a HTTP body. Note we're talking about HTTP, not HTML. The HTTP body is typically but not necessarily text/html content.
So as I just said, the page information goes in the HTTP body, but in order to perform a sendRedirect operation, the servlet container needs to write information to the HTTP header.

If you start writing to the HTTP body, then the container can buffer the response and hold off sending to the client in case you change your mind, but at some stage the buffer will fill and it will be too late to change your mind. Trigger an IllegalStateException. I have seen people increase the size of the buffer but this is hiding the problem, not solving it. It is better to get out of the habit beforehand. Also, increasing the size of the buffer will increase the memory that the application will need to run without any real benefit.
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David O'Meara wrote:You can't call sendRedirect if you have written to the output stream, or in your case the Writer.
It is bad practice to put HTML in a servlet, and if you gather the data ready to display and pass to a JSP then your code becomes:

You can call one or the other, but not both.


I am doing the same if you recheck please
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David O'Meara wrote:Taking it a step further, the HTTP response that you send back to the client is made up of a HTTP header and a HTTP body. Note we're talking about HTTP, not HTML. The HTTP body is typically but not necessarily text/html content.
So as I just said, the page information goes in the HTTP body, but in order to perform a sendRedirect operation, the servlet container needs to write information to the HTTP header.


My code is just sending the pageNo and catId as HTTP response not HTTP body

David O'Meara wrote:If you start writing to the HTTP body, then the container can buffer the response and hold off sending to the client in case you change your mind, but at some stage the buffer will fill and it will be too late to change your mind. Trigger an IllegalStateException. I have seen people increase the size of the buffer but this is hiding the problem, not solving it. It is better to get out of the habit beforehand. Also, increasing the size of the buffer will increase the memory that the application will need to run without any real benefit.

I not increase any buffer size but are you sure this is the case with my code?

Best regards
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anything written to the output stream or writer goes to the HTTP body. Your code is full of text written to the Writer and should be moved to a JSP instead.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you see "IllegalStateException" in a servlet (or JSP), look for where you're setting a response header, after writing to the reponse's PrintWriter. 99% of the time, this is the cause of the IllegalStateException.

On line 27:



... followed by a lot more printing to that PrintWriter. Then on line 186:



Your error log is even telling you that this is where the exception is occurring:



One quick solution to fix this code might be instead of writing directly to the response's PrintWriter, write to a local StringWriter as a buffer instead. Once all the logic that might send a redirect is complete, then write the StringWriter's contents out to the response's PrintWriter. The biggest advantage of that approach to you is that it would be relatively simple to change in your code:

... leave all the "out.println" statements the same ...






 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Nelson wrote:
One quick solution to fix this code might be instead of writing directly to the response's PrintWriter, write to a local StringWriter as a buffer instead. Once all the logic that might send a redirect is complete, then write the StringWriter's contents out to the response's PrintWriter. The biggest advantage of that approach to you is that it would be relatively simple to change in your code:

... leave all the "out.println" statements the same ...



Thanks from the bottom of my heart to post such precious opinion and helping me.

I applied your directives but its throwing 16 errors at compile time and the message is same:


Thanks again for helping

Best regards
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Bad! I missed a step! You need to wrap the StringWriter in a PrintWriter in order to get the println method. Try this instead:



Sorry to lead you astray! I had forgotten, all Writers are not created equal.
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now compiled and hopefully will fix the error.

Thanks from the bottom of my heart
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic