简体   繁体   English

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

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

Is there way to include error page for any wrong path request? 有没有办法为任何错误的路径请求包含错误页面? I need all wrong path requested by the user to be sent to error.jsp 我需要将用户请求的所有错误路径发送到error.jsp

For example if "/example" is part of the web.xml then it would send to localhost:8080/example If user specifies localhost:8080/examp then it would redirect to error.jsp 例如,如果“ / example”是web.xml的一部分,则它将发送到localhost:8080 / example如果用户指定localhost:8080 / examp,则它将重定向到error.jsp

I tried the following code in the web.xml and created a error.jsp inside Web Pages directory in Netbeans and it still sent me to yahoo error handle page: 我在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 error.jsp文件

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

First create a custom servlet and use it for both exceptions and errors. 首先创建一个自定义servlet,并将其用于异常和错误。 Refer the below code. 请参考以下代码。

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

Then configure it in deployment descriptor which is your web.xml 然后在您的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>

when a exception occures it will call the AppExceptionHandler class and then it will cause to display a custom error page. 当发生异常时,它将调用AppExceptionHandler类,然后将导致显示自定义错误页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM