This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher showing error?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher showing error??" Watch "RequestDispatcher showing error??" New topic
Author

RequestDispatcher showing error??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Guys,

I tried the code as below,



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)


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

But when I tried PrintWriter outp = response.getWriter(); instead of ServletOutpurStream, I'm getting the desired o/p??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

What happens if I do the forward using a jsp:forward say in a jsp page and If I use ServletOutputStream, will I still get IllegalStateException??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Any help ranchers?? Satou I was expecting your reply!
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Napu Sun
Ranch Hand

Joined: Apr 18, 2006
Posts: 33
Thanks, Sergio.
You made this very clear. I tested it works in the way what you pointed out: we cannot invoke getOutputStream and getWriter in the same response object.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Sergio,

When I tried this example by uncommenting the PrintWriter out = response.getWriter() and commented the outputstream, I'm able to run it without any errors...
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I tried the following,

The service method,



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???,





This is my test.jsp in both cases

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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???
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Guys,

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...
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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).
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Yeah, you are right sergio. Maybe I will try that. Can you summarize the whole thing related to RequestDispatcher and flush??
 
 
subject: RequestDispatcher showing error??