aspose file tools*
The moose likes Servlets and the fly likes how to  avoid the java.lang.IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to  avoid the java.lang.IllegalStateException" Watch "how to  avoid the java.lang.IllegalStateException" New topic
Author

how to avoid the java.lang.IllegalStateException

shyamkumar bopannachengalaiah
Ranch Hand

Joined: Dec 30, 2008
Posts: 100
I am working on a application where there is a file which contains a for loop where the i value checks for 5 values, but when the loop is running and when i==3 , the page is redirected automatically, at that time the system throws an java.lang.IllegalStateException.how can i overcome this?.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

shyamkumar bopannachengalaiah wrote:but when the loop is running and when i==3 , the page is redirected automatically,


How can something happen automatically . There must be some code to redirect the page...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

post your code. it will help to find out your mistake
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You cannot change the response anymore when it is already been committed (the headers are been sent to the client). A response is committed when one or more of the following conditions is met:
1) You have written more than 8KB (at least, the default buffer size) to the response, which implicitly invokes flush().
2) You have explicitly invoked flush() on the outputstream of the response after written a small amount of data to it.
3) You have invoked RequestDispatcher#forward() with it.
4) You have already invoked another sendRedirect() on it.

A sendRedirect() require a change of the response headers. So if you call sendRedirect() on a response which is already committed, you will get this exception.

Avoiding it is quite simple: write proper code.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This issue has been discussed here quite often so a search of this forum should give you plenty to read.

There is also an article in our FAQ section that covers some of the more common reasons for this.
http://faq.javaranch.com/java/IllegalStateException


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
shyamkumar bopannachengalaiah
Ranch Hand

Joined: Dec 30, 2008
Posts: 100
seetharaman venkatasamy wrote:post your code. it will help to find out your mistake






This is code you mentioned where i am getting java.lang.illegalstate exception only if the query condition return false and the response is redirected.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to avoid the java.lang.IllegalStateException