Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why IllegalStateException ?

 
ashwathi nair
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

>>public java.io.PrintWriter getWriter()
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object.

I wonder why the exception here is IllegalStateException ?
Is there any "state transition" happening during getOutputStream() method ?
Could some one plz tell what is actaully happening behind while calling these methods ?

Thanks,
Aswathi
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know, I can guess that Writer can already take some data and probabaly buffer them. If you get OutputStream then data written using Writer can get lost or appear in wrong place. You can think about other conditions, like mixing encoding and so on.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that if the getWriter or getOutputStream has already been called then response headers may already have been sent. Thus anything you write probably wont match the headers so you are in an illegal state.
Bill
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any "state transition" happening during getOutputStream() method ?


Well, yes, it looks kinda trivial but I guess you could see 3 response states:

State 1 : no output has yet been requested
State 2 : an output stream has already been requested
State 3 : a writer has already been requested


=> you can request a writer when you are in state 1 or 3 and then go to state 3 in both cases, or you can get an outputStream from state 1 or 2 and then go to state 2 in both cases.

You can't request a writer from state 2 or an output stream from state 3. If you do, you get an IllegalStateException.

Sounds logical to me.
 
ashwathi nair
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dema, Bill & Rodrigo for your replies.

Bill I agree with you. Same Exception during requestDispatcher.forward(rq,rs) can also be explained by this.

Rodrigo, your idea of state transition also can be applied here and the possible states could be 1) response not commited to client
2) response committed to clinet
Thanks,
Aswathi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!