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 are closed for "How to avoid IllegalStateException in java servlet?".
how will access the protected in other package.give me the explanation
nice blog. keep up the good work Joe
Good article Joe.
Test comments
thks Joe ,this helped me a lot i was having this problem for a while.
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
Thanks Joe
It is very helpful,maybe future I’d form a good habit of typing return statement after every redirect or forward action.
dude the members ie., variables or methods should be inherited to another class and then it can be used in other package
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
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?
Good blog. Keep up the good work!!
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.
hi this is balu ur information is good.
but when will comming to illegal exception in java
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???
it is most use full for java professionals
Thanks for the article. Can you please explain this with out using the Servlets. I mean in general Java Terms.
Thanks in advance.
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
I want to remove this site is boring
thanks..
Avoid java lang on my whatsapp