• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange behaviour of Tomcat

 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers

I just cleared SCJP and now working for SCWCD. In "Head First - Servlets & JSP" by K&B page# 137 it sats that-

We cant write to response and then call sendRedirect()
it will throw IllegalStateException

I tried both ways - by printing to a PrintWriter OR by writing to a ServletOutputStream on response object and then called sendRedirect()
- No exception is thrown and it redirects to new URL just fine.

Then I flushed the stream after writing to it and called sendRedirect()
- Now it just displays the written output but no redirecting occurred.

In both above cases no IllegalStateException occurred at runtime.

(I tried this on Tomcat 5.0.28) Any explanation regarding this conflict will be greatly appreciated.
Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before going further, I recommend you to install Tomcat 5.5 if you are studying for SCWCD. And don't forget that Tomcat is not a reference implementation, so it might sometimes behave differently. Apart from that, check Tomcat's localhost log and see if there's not an IllegalStateException there.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book says an IllegalStateException is thrown if you call sendRedirect() after the response has already been committed. In your case the response was not committed.

In fact, if you write something to the output stream without flushing, you are very very very likely that the response will not get committed. But there's always a chance that it could commit. Your attempt is a lot like holding a metal pole in the air during a lightning storm - you'll more than likely avoid getting hit but there's always that chance of *ZAP* and it simply isn't worth the consequences.

So, as the book says, "For practical purposes, it means you can't write to the response and then call sendRedirect()!" This doesn't mean that this will always throw an exception; it means you can't do this and expect your application to always work. For practical purposes, your application must always work, so you can't do it!
 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe and Marc.

I see Tomcat6 is also available. Should I go for Tomcat 5.5 or 6?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Tomcat6 is also available. Should I go for Tomcat 5.5 or 6?

Tomcat 6 supports Servlets 2.5 and JSP2.1. The exam is about Servlets 2.4 and JSP2.0 which Tomcat 5.5 supports.
Of course you could also use Tomcat 6 and make sure that web.xml is set at the correct version, but I'd recommend you to stick to Tomcat 5.5 instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic