Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When is IllegalStateException thrown

 
Pradeep Dhavakumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting confused about when a response is assumed to be commited . I thought calling the following methods after out.println("") will throw an Exception but a book says NO.Could someone please tell me whether an exception will be thrown/NOT thrown (and WHY? ) for the following cases:

1)
out.prinln(" ");
res.sendError( "");
out.prinln(" ");

2)
out.prinln(" ");
res.sendRedirect("");
out.prinln(" ");

3)

out.prinln(" ");
rd.forward(req,res);
out.prinln(" ");


Thanks ,
Pradeep
 
Vijay Albuquerque
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeep,
The following is from the J2EE Docs for HttpServletResponse.sendError() and HttpServletResponse.sendRedirect():

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Hence, the final output written should be considered to throw an IllegalStateException
[ June 21, 2004: Message edited by: Vijay Albuquerque ]
 
Mikalai Zaikin
Ranch Hand
Posts: 3371
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy !

Also, keep in mind that Tomcat servlet container has default output buffer (about 8 Kb AFAIR), the same I guess have other servlet containers.

So when you test your code - no exception may be thrown, because even you send something , this is not actually sent and is stored in output buffer. And exception is not thrown since response is not started yet.

To be safe, it is possible to try flush buffer - make sure response is commited. Or add more output data to force buffer flush.

hope, this helps,
MZ
 
Pradeep Dhavakumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay and Mikalai ,
Thanks for ur reply . So out.println(" ") first writes to the buffer and at this point resp is not commited and hence no exception is thrown .OK .

So the output of
out.println(" ");
rd.forward(req,resp);
out.println(" ");
depends on buffer size of the webserver!!

Was wondering if "ALL" web servers 'first' write to the buffer for out.println??...

Thanks,
Pradeep
 
Vijay Albuquerque
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeep,
The Servlet Spec states that a servlet container is allowed, but not required to buffer output going to the client.

VJ
 
Mikalai Zaikin
Ranch Hand
Posts: 3371
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Dhavakumar:
Vijay and Mikalai ,
Thanks for ur reply . So out.println(" ") first writes to the buffer and at this point resp is not commited and hence no exception is thrown .OK .

Was wondering if "ALL" web servers 'first' write to the buffer for out.println??...



Howdy !

I would assume if you see :



on the SUN's test - this means that response IS COMMITED. You should not rely on buffers, container-specific features, etc. Since as was already said - spec does not mandate using buffer, just an option.

cheers,
MZ
 
Pradeep Dhavakumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for clearing my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic