• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException help me

 
ramu av
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i'm using request.sendRedirect("url"); and i'm getting IllegalStateException.

please tell me why we get this Exception so that i can correct it.

here is the code snippet. please tell me what i should do.
 
haitham raik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ramu,
this type of exceptions occur in in one of the following two cases:
1. If the response was committed.
2. if a partial URL is given and cannot be converted into a valid URL.
so, to solve this problem follow the following steps:
1. validate the URL parameter to be relative to the current page and the page that you want to redirect to is exist and valid.
2. may be you have committed your output by using res.flush() method. if so you have to remove this statement from your code.
3. if your code placed in JSP page. I think you have used the page directive with attribute buffer="none".
3. if you have kept the buffer attribute without changing try to make it's value bigger, for example buffer="32kb"

the last thing that I want to say that the It is illegal to set autoFlush to false when buffer=none.

Regards
 
ramu av
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi haitham raik
response.sendRedirect("<target URL>");
is working fine for the first time execution by the server.
but on revisit to this servlet it results in IllegalStateException
i'm not using JSP so buffer issue does not come into picture. I have not even used res.flush();
So please help me out.
 
louise rochford
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramu,

is working fine for the first time execution by the server.
but on revisit to this servlet it results in IllegalStateException



Is it possible that the session has timed out? You can get an IllegalStateException if the session has become invalid. When you say "on revisit" do you mean when the redirected request comes in from the client, or when you invoke the method some time later?

Maybe you could post the top section of your stack trace so we can see the exception more clearly?

Regards,
Louise
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.sendRedirect("<target URL>");
is working fine for the first time execution by the server.
but on revisit to this servlet it results in IllegalStateException
i'm not using JSP so buffer issue does not come into picture. I have not even used res.flush();
So please help me out.

Alas - whether or not you are using JSP has nothing to do with it. It is the underlying servlet output buffer - which is flushed when full. The first flush of the output buffer sends the response headers - if you try to do a redirect AFTER the headers are sent, you get the IllegalStateException. This is because a redirect is done with the response headers.
Probably the second visit results in slightly more output, thus causing the flush.
Ideally your servlet should determine the necessity for a redirect before ANY output has been written. If this is impossible, set the response buffer size larger.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic