Hi, I purposely called RequestDispatcher.forward() after I already committed a response. I believe that doing so should throw an IllegalStateException. I think that the following code should be throwing an exception, but it actually lets me download the jar file BUT doesn't bring me to result.jsp.
The RequestDispatcher method understandably doesn't work in the code below, but my question is, shouldn't I get some sort of error statement? I even tried configuring the DD using the <error-page> tag but that doesn't seem to work either. I'm using tomcat 6.
I think, if you put "os.close()" before the point where you invoke the RequestDispatcher, you might get the desired behaviour.
al langley
Ranch Hand
Joined: Mar 28, 2008
Posts: 35
posted
0
Thanks for your replies! I tried moving the os.close() before the call to RequestDispatcher and I still get the same behavior, namely, I get a file download dialog which allows me to download and save the file and the RequestDispatcher doesn't work, but I don't get an error message either.
I'll keep on experimenting, to see if I am doing something wrong, if I figure something out, I'll post it.
Thanks for your help. [ April 14, 2008: Message edited by: al langley ]
Originally posted by Anirvan Majumdar: I think, if you put "os.close()" before the point where you invoke the RequestDispatcher, you might get the desired behaviour.
No. You should never close a stream that you did not open.
One thing that occurs to me is that the code is calling "flush" (or "close") on the underlying output stream itself, rather than on the response.
I don't think this is necessarily going to be recognized by the response as having "committed" it. Depending on how the implementation works internally, there's no guarantee that the response will keep track of everything you do directly to its internal elements. It might only know that the output has been flushed when it has done it itself.
I'd expect you to get the expected exception if the call is changed from "os.flush()" to "response.flushBuffer()".
You could also try using some calls to "response.isCommitted" to see whether/where the response actually becomes committed.
Mike
al langley
Ranch Hand
Joined: Mar 28, 2008
Posts: 35
posted
0
Thanks again for your suggestions. It seems that the response gets committed after a call to response.flushBuffer(). But still not getting an error thrown. Once again, thanks again for all who took the time to read the post. I'm going to keep on experimenting, if I figure something out, I'll post it.
[ April 14, 2008: Message edited by: al langley ]
Anirvan Majumdar
Ranch Hand
Joined: Feb 22, 2005
Posts: 261
posted
0
Originally posted by Bear Bibeault:
No. You should never close a stream that you did not open.
I thought "OutputStream os = response.getOutputStream();" opened the stream. I just advised him to move os.close() before getting the RequestDispatcher object.
It doesn't say not to stick bamboo shoots in your left eye either. Are you going to try that?
My point is, you shouldn't close streams that you don't open unless the documentation for the returning method explicitly requires it.
Anirvan Majumdar
Ranch Hand
Joined: Feb 22, 2005
Posts: 261
posted
0
Originally posted by Bear Bibeault: It doesn't say not to stick bamboo shoots in your left eye either. Are you going to try that?
My point is, you shouldn't close streams that you don't open unless the documentation for the returning method explicitly requires it.
Jeez. Why don't you back off a bit. For all the shenanigans of being a "Sheriff" out here, you surely need to exhibit a little more patience and courtesy. I wouldn't say this is the best manner in which one could prove his/her point on JavaRanch!
As for my suggestion to Al, he only wanted to verify a not so common behaviour. He was expecting to get an exception and he wasn't. I simply *suggested* something he could try. It seemed to me that closing the stream before instantiating the RequestDispatcher *might* work. If that was wrong you could very well just point out the same and also state a reason which can be verified! I think it's in the best spirit of this forum if people don't get feisty about their responses. In my honest opinion, knowledge sharing doesn't really floursih that way
al langley
Ranch Hand
Joined: Mar 28, 2008
Posts: 35
posted
0
I do appreciate all the responses, and just from this one post, I think I learned a lot.
I found a possible explanation (while reading HFSJ) of why I am not getting the exception message even though an exception occurs. To paraphrase, once the outputstream finishes writing and by the time the Container comes across the forward() method, its already too late for the Container to send anything else--including the IllegalStateException. The forward() won't do anything, but you won't see an error message either. So MAYBE, because of the nature of the exception, users won't see an IllegalStateExceptions message most of the time.