Java-EE重定向到自定义错误页面

时间:2014-06-03 01:02:36

标签: java jsp java-ee redirect error-handling

是否可以为任何错误的路径请求包含错误页面? 我需要将用户请求的所有错误路径发送到error.jsp

例如,如果" / example"是web.xml的一部分,然后它将发送到localhost:8080 / example 如果用户指定localhost:8080 / examp,那么它将重定向到error.jsp

我在web.xml中尝试了以下代码,并在Netbeans的Web Pages目录中创建了一个error.jsp,它仍然将我发送到yahoo错误句柄页面:

<web-app>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.jsp</location>
    </error-page>

</web-app>

error.jsp文件

<%@ page isErrorPage="true" %>
<html>
    <head>
        <title>Show Error Page</title>
   </head>
   <body>
        <h1>Opps...</h1>
        <p>An error occurred.</p>
   </body>
</html>

1 个答案:

答案 0 :(得分:2)

首先创建一个自定义servlet并将其用于异常和错误。请参阅以下代码。

@WebServlet("/AppExceptionHandler")
public class AppExceptionHandler 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 {
        // Analyze the servlet exception
        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";
        }

        // Set response content type
          response.setContentType("text/html");

          PrintWriter out = response.getWriter();
          out.write("<html><head><title>Exception/Error Details</title></head><body>");
          if(statusCode != 500){
              out.write("<h3>Error Details</h3>");
              out.write("<strong>Status Code</strong>:"+statusCode+"<br>");
              out.write("<strong>Requested URI</strong>:"+requestUri);
          }else{
              out.write("<h3>Exception Details</h3>");
              out.write("<ul><li>Servlet Name:"+servletName+"</li>");
              out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");
              out.write("<li>Requested URI:"+requestUri+"</li>");
              out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");
              out.write("</ul>");
          }

          out.write("<br><br>");
          out.write("<a href=\"index.html\">Home Page</a>");
          out.write("</body></html>");
    }
}

然后在部署描述符中配置它,这是您的web.xml

        <welcome-file-list>
               <welcome-file> index.html</welcome-file >
        </welcome-file-list>
        <error-page>
               <error-code> 404</error-code>
               <location> /AppExceptionHandler</location >
        </error-page>

        <error-page>
               <exception-type> javax.servlet.ServletException</exception-type>
               <location>/AppExceptionHandler</location >
        </error-page>
        <error-page>
  <exception-type >java.lang.Throwable </exception-type>
  <location >/AppExceptionHandler </location>

当发生异常时,它将调用AppExceptionHandler类,然后它将导致显示自定义错误页面。