I call the above servlet from my jsp called test.jsp and forward it using the requestdispatcher to the same jsp again. When I tried the above example, I got the following error...
java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599) org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:163) org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:122) org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:115) org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190) org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115) org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75) org.apache.jsp.test_jsp._jspService(test_jsp.java:67) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) com.example.ExampleServlet.service(ExampleServlet.java:28) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Ooof. This is a tough one. I'll try to answer it using the specs.
Accordingly to JSP.12.2.3:
The initial JspWriter object is associated with the PrintWriter object of the ServletResponse in a way that depends on whether the page is or is not buffered. If the page is not buffered, output written to this JspWriter object will be written through to the PrintWriter directly, which will be created if necessary by invoking the getWriter() method on the response object. But if the page is buffered, the PrintWriter object will not be created until the buffer is flushed and operations like setContentType() are legal. Since this flexibility simplifies programming substantially, buffering is the default for JSP pages.
And SRV.14.2.22 states:
getOutputStream() Throws: IllegalStateException - if the getWriter method has been called on this response
getWriter() Throws: IllegalStateException - if the getOutputStream method has already been called for this response object
In your case, what happened is that you first called ServletResponse.getOutputStream() in your servlet and then after forwarding to the jsp page, the jsp page tried to obtain the JspWriter which in turn is bounded to a PrintWriter object (this means that at some point there is a call to ServletResponse.getWriter()). In the end, you finished calling getOutputStream() and getWriter() on the same ServletResponse object. That's why you've got a IllegalStateException.
The same would be true if you forward from your jsp page to the servlet that uses the ServletOutputStream.
My conclusion would be: if you are forwarding from/to a jsp page, don't use ServletResponse.getOutputStream(). [ April 30, 2007: Message edited by: Sergio Tridente ]
I try to access this servlet from a jsp and I'm getting the o/p as "This is the output from the servlet". It should have thrown an IllegalStateException right as we are doing the forward after the response is comitted?? But at the same time when I try using the ServletOutputStream as below I'm getting the same o/p???,
If I don't use the line outp.flush(), then with the ServletOutputStream, I'm getting an IllegalState but when I use a PrintWriter (without the line outp.flush()), I'm not getting an IllegalState and I'm able to see the desired o/p?? WHY???
What is the whole catch with RequestDispatcher.forward, jsp:forward and the flush method call?? Can anyone summarize this here?? I could have summarized it in my SCWCD Hints page but still I'm dubious about flush() call and the ServletOutputStream...as I posted my question above...
First: getOutputStream() and getWriter() on the same ServletResponse object is a NO NO. Take a look at my first post to find the spec's citation.
Originally posted by Jothi Shankar Kumar Sankararaj: I try to access this servlet from a jsp and I'm getting the o/p as "This is the output from the servlet". It should have thrown an IllegalStateException right as we are doing the forward after the response is comitted?? But at the same time when I try using the ServletOutputStream as below I'm getting the same o/p???,
My bet is that it is throwing the exception but you are not seeing it in the browser because... the reponse had already been commited. Try writing to the console to see what happens (catch the exception and write to the console).