How to redirect to error page when exception occurs from servlet?

后端 未结 3 1173
终归单人心
终归单人心 2021-01-12 20:24

I am writing a servlet, in that if any exception occurs i donэt want to display exception/error message on browser, so I will redirect to my customized error page. So I have

相关标签:
3条回答
  • 2021-01-12 20:43

    Only way to handle it in a generic way is to use web.xml like below:

    <error-page>
      <exception-type>java.lang.Throwable</exception-type>
      <location>/ErrorHandler</location>
    </error-page>
    

    The servlet is thrown ServletException and IOException but if you want to handle runtime exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable. You can use multiple error-page entries that will handle different type of exceptions and have different handlers.

    Example:

    @WebServlet("/ErrorHandler")
    public class ErrorHandler extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            processError(request, response);
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            processError(request, response);
        }
        private void processError(HttpServletRequest request,
                HttpServletResponse response) throws IOException {
            //customize error message
            Throwable throwable = (Throwable) request
                    .getAttribute("javax.servlet.error.exception");
            Integer statusCode = (Integer) request
                    .getAttribute("javax.servlet.error.status_code");
            String servletName = (String) request
                    .getAttribute("javax.servlet.error.servlet_name");
            if (servletName == null) {
                servletName = "Unknown";
            }
            String requestUri = (String) request
                    .getAttribute("javax.servlet.error.request_uri");
            if (requestUri == null) {
                requestUri = "Unknown";
            }    
            request.setAttribute("error", "Servlet " + servletName + 
              " has thrown an exception " + throwable.getClass().getName() +
              " : " + throwable.getMessage());    
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        }
    }
    
    0 讨论(0)
  • 2021-01-12 20:49

    One way to handle it in a generic way is to use web.xml like below:

    <error-page>
        <exception-type>java.io.IOException</exception-type >
        <location>/ErrorHandler</location>
    </error-page>
    

    I have just included IO exception but you might have say SQLException you could very well add another error-page and another location for the same. Similarly you could say java.lang.Exception type and one handler handling everything.

    0 讨论(0)
  • 2021-01-12 20:59

    In some method, you would have the following:

    try {
      // something
    } catch (Exception e) {
      sendErrorRedirect(req, res, "/errorpage.jsp", e);
    }
    
    // then....  sendErrorRedirect looks like this:
      protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) {
          try {
                request.setAttribute ("javax.servlet.jsp.jspException", e);
                getServletConfig().getServletContext().getRequestDispatcher(errorPageURL).forward(request, response);
          } catch (Exception ex) {
                putError("serXXXXX.sendErrorRedirect ", ex);
          }
      }
    
    0 讨论(0)
提交回复
热议问题