tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Roberto Cosenza" <roberto.cose...@infoflexconnect.se>
Subject Re: IllegalStateException: getOutputStream() has already been called for this response
Date Tue, 08 Feb 2005 21:46:12 GMT
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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message