• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to avoid the java.lang.IllegalStateException

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post your code. it will help to find out your mistake
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic