Bookmark Topic Watch 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
API page for IllegalStateException

The most common cause of this exception is a servlet or JSP attempting to write to the output stream after the response has been committed.

For servlets, the easiest way to avoid this is to branch the code in your service method in such a way as to insure that calls to HttpServletResponse.sendRedirect or RequestDispatcher.forward are always the last call made before the end of the method. This can be achieved by adding a return call just after either of these.


Another way this can happen is by attempting to redirect from within the middle of a JSP page, eg:

Another common place where an IllegalStateException is likely to occur is in a JSP that attempts to stream binary data. JSPs are primarily designed to format output as HTML. With HTML, white space characters are ignored. It is not uncommon for JSP compilers to inject their own white space charaters to the beginning and/or end of the output stream. Line breaks in the developer's code can also be interpreted as white space in the output stream. These white space characters can interfere with the generated servlet's ability to create and stream the binary outputdata, resulting in the IllegalStateException.

The simple solution to this problem is to always use a servlet for streaming binary data.

NOTE: Tomcat 5.5.X seems to be lenient on throwing an IllegalStateException. Although the response has been flushed, it seems to allow the redirection, without throwing any exception. Refer to this thread

Another -completely unrelated- source of this exception is calling the remove method of JavaDoc:java.util.Iterator more than once between calls to next.

Related to the previous cause, and not specific to JSP/Servlets/SCWCD: you mustn't try to use the same Stream twice:-The Stream had been completely consumed by the end of line 4, so the attempt to reuse it in line 6 will cause an IllegalStateException to be thrown.

Instances of many classes can be closed and any attempt to use them afterwards will cause an IllegalStateException to be thrown. An example is java.util.Scanner, where about 50 methods can throw such an exception. This is also related to the last two cases; remember that Scanner implements Iterator<String>.

Back to the JspFaq ServletsFaq ScwcdFaq
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic