Servlet exception handler package com.ack.web.servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Map the location of the servlet to handle at error * with the supplied status code * ... javax.servlet.ServletException /servlet/com.ack.web.servlet.ServletExceptionHandler */ public class ServletExceptionHandler extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); String exceptionType = getRequestAttribute( req, "javax.servlet.error.exception_type", "status code not supplied" ); String errorMsg = getRequestAttribute( req, "javax.servlet.error.message", "error msg not supplied" ); // exception attribute available as of servlet 2.3 API Object exception = req.getAttribute( "javax.servlet.error.exception" ); pw.println( "

Custom Exception Handler

" ); pw.println( "

Exception type: " + exceptionType + "

" ); pw.println( "

Exception Message: " + errorMsg + "

" ); pw.println( "

Problem accessing: " + req.getRequestURI() + "

" ); // if we have the exception dump it out to the page if( exception != null ) { ByteArrayOutputStream bos = null; try { pw.println( "


" );
        bos = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter( bos, true );
        ( (Exception) exception ).printStackTrace( writer );
        pw.println( bos.toString() );
      }
      finally {
        pw.println( "
" ); if( bos != null ) bos.close(); } } // output date and time of error pw.println( "


" ); pw.println( new java.util.Date( System.currentTimeMillis() ) ); } private String getRequestAttribute( HttpServletRequest req, String name, String defaultValue ) { Object value = req.getAttribute( name ); return ( value != null ) ? value.toString() : defaultValue; } }