Customized Error Page for java application
Sometimes application will throw exception page. thinking of addin a custom f开发者_JAVA百科riendly error page which user can report to us then we can trace easily.
Initially idea is to add a issue ID, date & time.
Anyone have any suggestions?
Assuming Tomcat Servlet container:
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.
<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
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.
If you put this at the top of your JSP pages:
<%@ 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:
<%@ 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
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.
精彩评论