The forward method of the RequestDispatcher interface may be called by the calling servlet only when no output has been committed to the client. If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet’s service method is called. If the response has been committed, an IllegalStateException must be thrown. "does this mean I should have no out.println stmt before calling requestDispatcher.forward() and after calling the forward"??? please someone clear my doubt
Craig Jackson
Ranch Hand
Joined: Mar 19, 2002
Posts: 405
posted
0
What I believe it means is that you should check the response with: ServletResponse.isCommitted() method prior to forwarding of the request, at that point processing ends for this request within this servlet. craig
Hi, Besides, I tried the code: out.println("##"); RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet/aView"); dispatcher.forward(req, res); out.println("!!"); It works, but there's no printout either "##" or "!!". Why
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
out.println("##"); "##" is stored in the output buffer (but not yet sent to the browser). RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet/aView"); dispatcher.forward(req, res); The output buffer is cleared, removing "##". Then "/servlet/aView" is called, its response is committed and the output stream is closed. out.println("!!"); An IOException is thrown because "out" is closed. By the way, there is no need to call isCommitted() if you know that the response will not have been committed yet, i.e. if you have generated (almost) no output and have not called any methods that would commit the response. - Peter
Craig Jackson
Ranch Hand
Joined: Mar 19, 2002
Posts: 405
posted
0
The reason why you are not getting an IOException message or the output "!!" is because after the request and response has been forwarded to the the url, processing ends for this request within this serlvet.
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Well, processing doesn't really end; it continues, but the output stream has been closed and nothing will be sent to the client anymore (as you pointed out, any exceptions thrown will be effectively invisible). Is this merely an academic point? Well, I could imagine situations in which perceived response time is improved by first sending a response to the client and then performing some further processing. - Peter [ April 07, 2002: Message edited by: Peter den Haan ]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.