tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From DAVID TURNER <DTUR...@nhbb.com>
Subject Re: IllegalStateException: getOutputStream() has already been called for this response
Date Wed, 09 Feb 2005 13:24:05 GMT
Thanks for the advice Roberto.  I agree that the approach is wrong, but in 
my real application I definitely use the MVC pattern as much as possible. 

The problem I'm running into is that my servlet calls other classes to do 
the business logic.  One of these classes does XSLT.  What I end up doing 
is getting the OutputStream from the servlet and passing it down to the 
class that does the transformation, which sends the results of the 
transformation to this OutputStream.  Exceptions could possible occur 
during the transformation, and this is where I run into this problem of 
the jsp error page not getting called.  The use of OutputStream or Writer 
for the XSLT results is what keeps me from doing exactly what you 
suggested.  I have this one hook, so to speak.

I welcome any suggestions/work-arounds other than what I'm doing now with 
the use of the OutputStream in the XSLT transformation. 

Thanks.





"Roberto Cosenza" <roberto.cosenza@infoflexconnect.se> 
02/08/2005 04:46 PM
Please respond to
"Tomcat Users List" <tomcat-user@jakarta.apache.org>


To
"Tomcat Users List" <tomcat-user@jakarta.apache.org>
cc

Subject
Re: IllegalStateException: getOutputStream() has already been called for 
this response






Your approach is bad but don't worry. If you have time, read something 
about MVC. You will need it eventually.

To make your servlet work,  try something like:
public void doPost(HttpServletRequest req, HttpServletResponse res) 
                                   throws ServletException, IOException { 
 
    StringBuffer myOutput = new StringBuffer();
 
        try { 
                myOutput.append("Here I'm not doing anything illegal");    
 
 
                    if(true) 
                        throw new BookNotFoundException("Book doesn't 
exist"); 
        } 
        catch (Exception e) { 
                  System.out.println("Caught exception: " + 
e.getMessage()); 
                    throw new ServletException("Dummy Exception", e); 
        }

// you reach this only if no errors have been caught so no error page to 
display
        OutputStream out = res.getOutputStream(); 
        res.setContentType("text/plain"); 
        out.println(myOutput.toString()); 
 
 
}
/roberto




  ----- Original Message ----- 
  From: DAVID TURNER 
  To: tomcat-user@jakarta.apache.org 
  Sent: Tuesday, February 08, 2005 9:56 PM
  Subject: IllegalStateException: getOutputStream() has already been 
called for this response 



  I'm trying to write a servlet that handles business logic exceptions by 
specifying in the web.xml the jsp error page that I want to use for a 
specific Exception (see web.xml snippet below).  I have this working when 
I use response.getWriter() in the servlet instead of 
response.getOutputStream()  -- see sample servlet code below.  But, when I 
try to use the response.getOutputStream() approach the jsp error page 
doesn't work and an "IllegalStateException: getOutputStream() has already 
been called for this response " gets thrown because the jsp is probably 
trying to get the OutputStream also. 

  Why does the response.getWriter() method work even after headers/data 
have been written to the writer? 

  Is there any way to get the jsp error page to work with the 
getOutputStream()? 

  I would like to eventually compress the response stream, but from all 
the examples I've come across on compression they all use getOutputStream. 





  web.xml contents: 

      <error-page> 
              <exception-type>BookNotFoundException</exception-type> 
              <location>/jsp/ErrorPage.jsp</location> 
      </error-page> 





  servlet contents: 

  public void doPost(HttpServletRequest req, HttpServletResponse res) 
                                     throws ServletException, IOException 
{ 
                  PrintWriter out = res.getWriter(); 

              //OutputStream out = res.getOutputStream(); 

          res.setContentType("text/plain"); 
 
          try { 
                  out.println("Line 1 of servlet"); 
 
                          //out.write("Line 1 of servlet".getBytes()); 
 
                          throw new BookNotFoundException("Book doesn't 
exist"); 
          } 
          catch (Exception e) { 
                  res.reset(); 
                    System.out.println("Caught exception: " + 
e.getMessage()); 
                      throw new ServletException("Dummy Exception", e); 
          } 
 
  }


------------------------------------------------------------------------------


  ---------------------------------------------------------------------
  To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
  For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message