java 自定义错误页面

http://stackoverflow.com/questions/2748220/how-to-show-user-friendly-error-page-instead-of-tomcat-log-with-stack-trace-in-br


just declare an <error-page> in web.xml wherein you can specify the page which should be displayed on a certain Throwable (or any of its subclasses) or a HTTP status code. E.g.

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error.jsp</location>
</error-page>

which will display the error page on any subclass of the java.lang.Exception, but thus notjava.lang.Throwable or java.lang.Error. This way you can have your own error page for any kind of Throwable. E.g. java.sql.SQLExceptionjava.io.IOException and so on.

Or,

<error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
</error-page>

which whill display the error page on a HTTP 500 error, but you can also specify another ones for 404 (Page Not Found), 403 (Forbidden), etcetera.

If you declare <% page isErrorPage="true" %> in top of error.jsp, then you have access to the thrown Exception (and thus also all of its getters) by ${exception} in EL.

<p>Message: ${exception.message}

n your web.xml:

<error-page>
  <error-code>500</error-code>
  <location>/errorpages/500.jsp</location>
</error-page>

You may also catch specific exceptions or exceptions which extend Throwable:

<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/errorpages/500.jsp</location>
</error-page>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值