FileDocCategorySizeDatePackage
ErrorDispatcherServlet.javaAPI DocExample3566Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.servlets

ErrorDispatcherServlet

public class ErrorDispatcherServlet extends HttpServlet
This class can be registered as an error handler for exceptions and status codes in the web application deployment description. It forwards to a JSP error page after setting all request attributes the JSP container uses to initialize the exception variable, as well as an attribute to contain the URI for the request that triggered the error handler.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private static final String
SERVLET_EXCEPTION
private static final String
JSP_EXCEPTION
private static final String
SERVLET_ERROR_SOURCE
private static final String
JSP_ERROR_SOURCE
private static final String
HTTP_STATUS_CODE
private String
errorPage
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Calls doPost()


        doPost(request, response);
    
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Sets the request attributes for the exeception and request URI and forwards to the JSP error page.


        /*
         * Try to get the servlet exception and save it in the
         * attribute expected by a JSP error page if found
         */
        Object t = request.getAttribute(SERVLET_EXCEPTION);
        if (t != null) {
            request.setAttribute(JSP_EXCEPTION, t);
        }
        else {
            /* 
             * If there's a status code, create an Exception with the
             * status code so the JSP error page can get it.
             */
            Object statusCode = request.getAttribute(HTTP_STATUS_CODE);
            if (statusCode != null) {
                request.setAttribute(JSP_EXCEPTION, 
                    new Exception("HTTP status code: " + statusCode));
            }
        }

        /*
         * The URI of the page causing the problem is passed as
         * a request attribute for servlets. If it's there, save
         * it in the request attribute the JSP page uses instead.
         */
        Object errorSource = request.getAttribute(SERVLET_ERROR_SOURCE);
        if (errorSource != null) {
            request.setAttribute(JSP_ERROR_SOURCE, errorSource);
        }

        ServletContext context = getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher(errorPage);
        rd.forward(request, response);
    
public voidinit()
Reads the "errorPage" init parameter and saves the value in an instance variable. The value must be a context-relative path to a JSP error page.

exception
UnavailableException if "errorPage" is not set.


                                         
         
        ServletConfig config = getServletConfig();
        errorPage = config.getInitParameter("errorPage");
        if (errorPage == null) {
            throw new UnavailableException("errorPage not defined");
        }
        getServletContext().log("errorPage: " + errorPage);