How to avoid IllegalStateException in java servlet?

Last modified on August 1st, 2014 by Joe.

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....
*/
}

Comments on "How to avoid IllegalStateException in java servlet?"

  1. Aamathkhan says:

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

  2. AJ says:

    nice blog. keep up the good work Joe

  3. Gopikrishna says:

    Good article Joe.

  4. Anonymous says:

    Test comments

  5. ab says:

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

  6. Harsha says:

    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

  7. Vinuraj says:

    Thanks Joe

  8. benson says:

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

  9. Abhijit says:

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

  10. deepan says:

    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

  11. Anonymous says:

    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?

  12. Halley says:

    Good blog. Keep up the good work!!

  13. Mahanth says:

    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.

  14. Balakrishna says:

    hi this is balu ur information is good.
    but when will comming to illegal exception in java

  15. shanu says:

    Thanx for solution its works for me…..but why this problem is occurring plz explain this also and what we do if such problem persist on jsp???

  16. venu says:

    it is most use full for java professionals

  17. Prethesh Kumar Bhalotia says:

    Thanks for the article. Can you please explain this with out using the Servlets. I mean in general Java Terms.

    Thanks in advance.

  18. Anonymous says:

    Hi Joe,
    I am working on Java stand alone application. How do I effectively use HttpURLConnection multiple times?
    I have tried this in my code and it displays a IllegalStateException (connection already exists) error though I am disconnecting the previous connection.
    Thank you,
    Narendra

  19. warar says:

    I want to remove this site is boring

  20. AK says:

    thanks..

  21. Nana says:

    Avoid java lang on my whatsapp

Comments are closed for "How to avoid IllegalStateException in java servlet?".