Two Laptop Bag*
The moose likes Servlets and the fly likes java.lang.IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.IllegalStateException" Watch "java.lang.IllegalStateException" New topic
Author

java.lang.IllegalStateException

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Mar 06, 2001
Posts: 13459


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

Joined: Aug 30, 2010
Posts: 147

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







OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Aug 30, 2010
Posts: 147

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

Joined: Mar 22, 2008
Posts: 732
Now compiled and hopefully will fix the error.

Thanks from the bottom of my heart
 
Don't get me started about those stupid light bulbs.
 
subject: java.lang.IllegalStateException
 
Similar Threads
Pagging Problem
Pagging Problem
Want to convert my code in method
NumberFormatException: For input string: "null"
getter and setters