How to avoid IllegalStateException in java servlet?

19/05/2008

The root cause of IllegalStateException exception is a java servlet is attempting to write to the output stream (response) after the response has been committed.

It is always better to ensure that no content is added to the response after the forward or redirect is done to avoid IllegalStateException. It can be done by including a ‘return’ statement immediately next to the forward or redirect statement.

Example servlet source code snippet:

public void doGet(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
if("success".equals(processLogin())) {
response.sendRedirect("menu.jsp");
return; // <-- this return statement ensures that no content is adedd to the response further
}

Note: This same scenario of IllegalStateException is applicable in JSP also.

/*
other servlet code that may add to the response….
*/
}

Ads by Google

15 comments on “How to avoid IllegalStateException in java servlet?

  1. how will access the protected in other package.give me the explanation

    • dude the members ie., variables or methods should be inherited to another class and then it can be used in other package

  2. thks Joe ,this helped me a lot i was having this problem for a while.

  3. hey i have the same problem, i am spawning a thread to upload attachments to amazons3 bucket , but i am getting java.lang.IllegalStateException: File has been moved – cannot be read again for some attachments,i am using java language spring framework. please do help me

  4. It is very helpful,maybe future I’d form a good habit of typing return statement after every redirect or forward action.

  5. Joe, I’m getting ‘IllegalStateException: setBufferSize()’ in my application. When I googled, they mentioned somewhere i’m setting the bufferSize after response committed but no where i’ve set in manually. I’m using JSF. Please help me

  6. Hi, what is the need of return after forward or sendRedirect? Won’t the control be left the current servlet/jsp at that point (where forward or sendRedirect is called).

    Can you please explain in which cases we might need such return statement?

  7. illegalstateException is a general exception,it is not only for the servlet or JSP ,please change this statement “The root cause of IllegalStateException exception is a java servlet is attempting to write to the output stream (response)”. As a professional java programmer we need to give correct definition. Sun definition “Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.” and then a example. One example here is : Iterator it2 = vect1.iterator();
    while(it2.hasNext())
    {
    Object obj2 = it2.next();
    it2.remove();
    }
    this also throws illegalstateException.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ABOUT
I am Joe, author of this blog. I run javapapers with loads of passion. If you are into java, you may find lot of interesting things around.
Ads by Google
STAY in TOUCH:

Email:

Core Java | Servlet | JSP | Design Patterns | Android | Spring | Web Service | © 2008-2012 javapapers.