【问题标题】:Java-EE redirect to a custom error pageJava-EE 重定向到自定义错误页面
【发布时间】:2014-07-23 05:23:24
【问题描述】:

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

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

我在 web.xml 中尝试了以下代码,并在 Netbeans 的网页目录中创建了一个 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>

【问题讨论】:

  • 向我们展示error.jsp 页面,您的配置看起来不错
  • 只有当您的服务器实际收到请求时,该错误页面配置才会起作用。显然,这里不是这种情况。
  • 我应该使用自定义错误页面处理错误的路径还是让它进入通用的雅虎错误处理? web.xml 是否会进行某种过滤以重定向所有错误的页面请求?

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


【解决方案1】:

首先创建一个自定义 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 类,然后会导致显示自定义错误页面。

【讨论】:

    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 2022-10-12
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多