• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

RequestDispatcher showing error??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But when I tried PrintWriter outp = response.getWriter(); instead of ServletOutpurStream, I'm getting the desired o/p??
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help ranchers?? Satou I was expecting your reply!
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you are right sergio. Maybe I will try that. Can you summarize the whole thing related to RequestDispatcher and flush??
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic