java应用程序的自定义错误页面

时间:2022-10-10 16:53:58

Sometimes application will throw exception page. thinking of addin a custom friendly error page which user can report to us then we can trace easily.

有时应用程序会抛出异常页面。考虑添加一个自定义友好的错误页面,用户可以向我们报告,然后我们可以轻松追踪。

Initially idea is to add a issue ID, date & time.

最初的想法是添加问题ID,日期和时间。

Anyone have any suggestions?

有人有什么建议吗?

2 个解决方案

#1


0  

Assuming Tomcat Servlet container:

假设Tomcat Servlet容器:

You can define your own custom error pages in your web.xml file. In the example shown below, we define 2 web pages -- server_error.html and file_not_found.html -- which will be displayed when the server encounters an error 500 or an error 404 respectively.

您可以在web.xml文件中定义自己的自定义错误页面。在下面显示的示例中,我们定义了2个网页 - server_error.html和file_not_found.html - 当服务器分别遇到错误500或错误404时将显示这些网页。

 <error-page>
    <error-code>500</error-code>
    <location>/server_error.html</location>
 </error-page>
 <error-page>
    <error-code>404</error-code>
    <location>/file_not_found.html</location>
 </error-page>   

Reference: http://linux-sxs.org/internet_serving/c581.html

#2


0  

The best way to handle errors will depend on your servlet container (Tomcat, WebSphere, etc). But there is a generic way to handle JSP errors which you should work on any container.

处理错误的最佳方法取决于您的servlet容器(Tomcat,WebSphere等)。但是有一种处理JSP错误的通用方法,你应该在任何容器上工作。

If you put this at the top of your JSP pages:

如果将它放在JSP页面的顶部:

<%@ page errorPage="errorpage.jsp"%>

then if an error occurs on that JSP page the user will be redirected to errorpage.jsp. You can then put the following page directive in your error page JSP:

然后,如果在该JSP页面上发生错误,则用户将被重定向到errorpage.jsp。然后,您可以将以下页面指令放在错误页面JSP中:

<%@ page isErrorPage="true" %>

That makes an javax.servlet.jsp.ErrorData object available to the error page code, which you could log along with an ID, date & time. Read more at http://download.oracle.com/javaee/5/tutorial/doc/bnahe.html#bnahi

这使得javax.servlet.jsp.ErrorData对象可用于错误页面代码,您可以将其与ID,日期和时间一起记录。更多信息,请访问http://download.oracle.com/javaee/5/tutorial/doc/bnahe.html#bnahi

However as suggested by The MYYN in another answer, depending on your framework there are probably other options for error handling, and they might be more robust.

但是,正如MYYN在另一个答案中所建议的那样,根据您的框架,可能还有其他错误处理选项,它们可能更强大。

#1


0  

Assuming Tomcat Servlet container:

假设Tomcat Servlet容器:

You can define your own custom error pages in your web.xml file. In the example shown below, we define 2 web pages -- server_error.html and file_not_found.html -- which will be displayed when the server encounters an error 500 or an error 404 respectively.

您可以在web.xml文件中定义自己的自定义错误页面。在下面显示的示例中,我们定义了2个网页 - server_error.html和file_not_found.html - 当服务器分别遇到错误500或错误404时将显示这些网页。

 <error-page>
    <error-code>500</error-code>
    <location>/server_error.html</location>
 </error-page>
 <error-page>
    <error-code>404</error-code>
    <location>/file_not_found.html</location>
 </error-page>   

Reference: http://linux-sxs.org/internet_serving/c581.html

#2


0  

The best way to handle errors will depend on your servlet container (Tomcat, WebSphere, etc). But there is a generic way to handle JSP errors which you should work on any container.

处理错误的最佳方法取决于您的servlet容器(Tomcat,WebSphere等)。但是有一种处理JSP错误的通用方法,你应该在任何容器上工作。

If you put this at the top of your JSP pages:

如果将它放在JSP页面的顶部:

<%@ page errorPage="errorpage.jsp"%>

then if an error occurs on that JSP page the user will be redirected to errorpage.jsp. You can then put the following page directive in your error page JSP:

然后,如果在该JSP页面上发生错误,则用户将被重定向到errorpage.jsp。然后,您可以将以下页面指令放在错误页面JSP中:

<%@ page isErrorPage="true" %>

That makes an javax.servlet.jsp.ErrorData object available to the error page code, which you could log along with an ID, date & time. Read more at http://download.oracle.com/javaee/5/tutorial/doc/bnahe.html#bnahi

这使得javax.servlet.jsp.ErrorData对象可用于错误页面代码,您可以将其与ID,日期和时间一起记录。更多信息,请访问http://download.oracle.com/javaee/5/tutorial/doc/bnahe.html#bnahi

However as suggested by The MYYN in another answer, depending on your framework there are probably other options for error handling, and they might be more robust.

但是,正如MYYN在另一个答案中所建议的那样,根据您的框架,可能还有其他错误处理选项,它们可能更强大。