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

后端 未结 3 1176
终归单人心
终归单人心 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: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);
          }
      }
    

提交回复
热议问题