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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to