簡體   English   中英

Java-EE重定向到自定義錯誤頁面

[英]Java-EE redirect to a custom error page

有沒有辦法為任何錯誤的路徑請求包含錯誤頁面? 我需要將用戶請求的所有錯誤路徑發送到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>

首先創建一個自定義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類,然后將導致顯示自定義錯誤頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM