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

标签 java jsp jakarta-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>

错误.jsp

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

最佳答案

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

关于Java-EE 重定向到自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005607/

相关文章:

java - CardBuilder Layout.AUTHOR 切换元素顺序

具有请求和 session 范围的 Java Bean

java - 有没有办法知道 ServletContextListener 中 Web 应用程序发生了什么变化?

java - Web应用的Ping实现方法

java - Eclipse 调试运行方法

java - 一般而言,管理调试消息的更好方法是什么?

Java 11 发送没有正文的 HTTP post 请求

Spring-mvc 3.0 带有复选框问题的 CRUD

mysql - 使用 JSP 和 Servlet POST 方法查询 MySQL

jakarta-ee - com.google.gwt.user.client.rpc.StatusCodeException : 404