当servlet发生异常时,如何重定向到错误页面? [英] How to redirect to error page when exception occurs from servlet?

查看:450
本文介绍了当servlet发生异常时,如何重定向到错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个servlet,因为如果发生任何异常,我不想在浏览器上显示异常/错误消息,因此我将重定向到我的自定义错误页面.所以我这样做是这样的:

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 done like this:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
try{
    //Here is all code stuff
}catch(Exception e){

  request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
  e1.printStackTrace();

}

这是正确的方法吗,如果我错了,请纠正我;如果有更好的机制,请告诉我.

Is this the correct way, if I am wrong please correct me and if there is any better mechanism please tell me.

推荐答案

以通用方式处理它的唯一方法是使用web.xml,如下所示:

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>

该servlet被抛出ServletExceptionIOException,但是如果您想在单个异常处理程序中处理运行时异常和所有其他异常,则可以提供异常类型作为Throwable.您可以使用多个错误页面条目,这些条目将处理不同类型的异常并具有不同的处理程序.

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.

示例:

@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);
    }
}

这篇关于当servlet发生异常时,如何重定向到错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆