• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to avoid the java.lang.IllegalStateException

 
shyamkumar bopannachengalaiah
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your code. it will help to find out your mistake
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shyamkumar bopannachengalaiah
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic