tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Cherkassky <>
Subject Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?
Date Wed, 29 Apr 2009 18:57:02 GMT
A long question:

First, I have a Servlet that writes to response.getOutputStream().  Here's a snippet:
  public class AuditTrailServlet extends HttpServlet {
    public void doGet( HttpServletRequest request, HttpServletResponse response )
      response.setContentType( "application/" );
      response.setHeader( "Content-Disposition", "attachment; filename=" + fileName );

      ResultSet rs = ...;
      for ( int row = 0 ;; row++ ) {
        // iterate over each row in the ResultSet
        for( int col = 0; fieldNameIt.hasNext(); col++ ) {
          // iterate over each column in the results, write to spreadsheet
          // custom code.
          // let us assume this block has a bug that occasionally throws a NullPointerException,

          // but only after a bunch of cells are written first.
          response.getOutputStream().write( /* excel byte[] */ );
          // more custom code.
      response.setStatus( HttpServletResponse.SC_OK );

Second, I have the following in the web.xml file, to tell tomcat to use a custom error page:

Last, here's myError.jsp:
  <%@ page isErrorPage='true' %>
  <h1>System Error</h1>
  You have encountered a system error.
  <%-- custom error processing that can't be done in a static .html page --%>

So, it all seems standard and straight-forward, right?

However, instead of seeing a nice custom error page when the NPE happens, I get the "ugly"
Tomcat error page, and
the following in the Tomcat logs:
  java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.ResponseBase.getWriter(
  at org.apache.catalina.connector.ResponseFacade.getWriter(
  at org.apache.jasper.runtime.JspWriterImpl.initOut(
  at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(
  at org.apache.jasper.runtime.PageContextImpl.release(
  at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(
  at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(
  at org.apache.jsp.error_jsp._jspService(

Yes, I know -- I *am* using Tomcat 4.1 (rather than the latest and greatest) for various legacy
and political reasons.

But my question is this:
- Is this a known bug?  Or am I doing something wrong with either the servlet, the web.xml
or with the jsp page?

Dave Cherkassky
  VP of Software Development
  DJiNN Software Inc.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message