Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redirect Error

 
Pol Appan
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am redirecting to page after doing some process, however I am getting error:


IllegalStateException: Response has already been committed, be sure not to write to the OutputStream or to trigger a commit due to any other action before calling this method.

How can I solve this?

Thanks in advance.

Cosmos
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you redirect before anything has been written, including headers.
Then return right after the sendRedirect.
 
Pol Appan
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried giving a return after the redirect, but still it throws that error.

I have lot of html statements before the redirect, does that cause any problem?

Cosmos
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had better come up with code snippet.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

U said that there are lot of html statements before redirect. This means that you are writting to the response. There may be a situation in your case that the response is alredy committed ( output is written to the response and it is flushed). Therfore it is throwing an Exception.

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you redirect before anything has been written, including headers.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a response wrapper to cache the response data in memory (ByteArrayOutputStream) and write it out at the end when you're sure that everything went as planned.
 
Pol Appan
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I will try or I will remove all html code and see whether it works or not, I beleiev if it's only JSP code, it should work without throwing any excpetions.

Cosmos
 
Pol Appan
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I removed all the html codes and it worked. I am using this page only for inserting and for calling a stored procedure.

Thanks to everyone for all help. Much appreciated.

Cosmos
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic