I do not understand why I am getting an exception , for me I should not because because there is no out.flush() or response.flushBuffer();
see also HFSJ, on page 850(question 45), in mock exam answers, they said this should be working
I don't find any details if java doc that PrintWriter buffers the output. The java doc for PrintWriter obtained from ServletResponse is as following, which clearly says that we need to call the flush() method explicitly to commit the response::
So I am also confused with above behavior.
Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Creator of Enthuware JWS+ V6
By default a PrintWriter is created without automatic flushing (see constructor in API), but the same can be said about a ServletOutputStream, so the problem is caused by something else. If we read the exception message well it says: "getOutputStream() has already been called for this response"
I tested it and it looks like the problem appears in the initialization of the forwarded JSP. It seems that the initialization of the JSP does a getOutputStream() and the container detects that this has already been done.
Bottom line: don't do anything with the response if you are going to do a forward. As the specs don't say what should happen: it might work but it also might not work...
Hi Sanjay ,
As Frits Walraven correctly told , when you are forwarding the request using request dispatcher , donot play with the response. The purpose of request dispatcher is that if the current servlet or jsp cannot handle the task on its own, delegate it to another jsp or even servlet using request dispatcher . Hence let the target jsp or servlet create the required response. Yeah there are absurdities produced when you try to work otherwise.