aspose file tools*
The moose likes Servlets and the fly likes How to avoid IllegalStateException (Response Already Commited ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to avoid IllegalStateException (Response Already Commited )" Watch "How to avoid IllegalStateException (Response Already Commited )" New topic
Author

How to avoid IllegalStateException (Response Already Commited )

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

How to avoid IllegalStateException (Response Already Commited )

Hi

I am having a jsp page in which , depending upon the action from the user i need to transfer the control to a different page

For example




But unfortunately i am getting IllegalStateException (Response Already Commited )

I had tried using RequestDispatcher , jsp:forward , sendRedirect , but everything results in same Exception


Please tell me what how can i successfly transfer the control

Anybody help from you would be greatly useful


Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can't if the response is already committed. Sounds like you are trying to implement your control logic in the wrong place.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

means what are the steps i need to follow to make it that code executed .please tell me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You need to move your code to a place where it can run before the response has been commited.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



THnaks paul , THe jsp page is developed in 2006 and it nearly consists of 3000 lines of code .

I don't know what to do .
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

well, a 3000 line JSP file is a huge problem that you should probably fix before moving forward. Use the jsp only for output. Try to put things like redirects in the servlet code.

"Response Already Committed" is telling us that the response has already started to write and you are trying to modify it. So, whatever you are trying to do, you must do it earlier, before anything has been written to the response.

look at this for more info
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
A common cause of this problem is simply having a space or carriage return at the top of the JSP. For example, if you have some page directives separated by carriage returns, then those may be put into the response body. Then, if your JSP tries to set something in the header, such as the document type, an exception is thrown. (Remember that the response is just a stream -- the JSP container can't back up and write header data after the page has already started writing the body.) So verify that there are no spaces or carriage returns anywhere in the page above the scriptlet. (There can be carriage returns and spaces within the page directives or scriptlets.) Don't even start the scriptlet "<%" on its own line, since that would mean you'd have a carriage return in the page before the scriptlet code is encountered.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Max, please let me know what is meant by carriage returnds ??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ravi Kiran Varre wrote:Max, please let me know what is meant by carriage returnds ??

The "return" key. A blank line. Vertical whitespace. CR/LF.

The nutshell version is that you can't redirect/etc. after something's been written to the response, including whitespace.

Echoing an earlier comment: a 3000-line JSP is a horrible idea, and should be fixed, or you will simply continue having difficult-to-diagnose and difficult-to-repair problems.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Paul, David ,Max ,Tim for suggestions.

I somehow solved it by using



kept this in the middle of the jsp

Most common problem so wanted to share the code
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

So in the middle of a 3000-line JSP, you automatically submit the form without the page having much, if any time to be displayed? What's the point of the rest of that JSP? Is it all Java code?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I mean in midle of the page means

if(action.equals("blowup")
{
---
---some code

<script>
document.formname.action="jsp page ";
</script>

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid IllegalStateException (Response Already Commited )