`

在web.xml中配置异常处理页面

    博客分类:
  • JSP
阅读更多
通常为了给用户提供良好的人机接口,我们都会为整个web应用,提供处理异常或者 错误的通用页面,而这些通用页面需要在web.xml中进行配置,主要是两种方式:其一根据HTTP响应状态码;其二是根据异常类名进行配置。

格式如下:

<web-app>

  <error-page>

     <exception-type>完整的异常类名</exception-type>

     <location>以”/”开头的异常处理页面路径</location>

  </error-page>

  <error-page>

     <error-code>HTTP响应状态码</error-code>

     <location>以”/”开头的异常处理页面路径</location>

  </error-page>

</web-app>

//--------------------------------------------------------------//


<!-- 400错误 请求无效 -->

    <error-page>

        <error-code>400</error-code>

        <location>/error.jsp</location>

    </error-page>

<!-- 404 页面不存在错误 -->

    <error-page>

        <error-code>404</error-code>

        <location>/error.jsp</location>

    </error-page>

   <!-- 500 服务器内部错误 -->

    <error-page>

        <error-code>500</error-code>

        <location>/error.jsp</location>

    </error-page>

    <!-- java.lang.Exception异常错误,依据这个标记可定义多个类似错误提示 -->

    <error-page>

        <exception-type>java.lang.Exception</exception-type>

        <location>/error.jsp</location>

    </error-page>

    <!-- java.lang.NullPointerException异常错误,依据这个标记可定义多个类似错误提示 -->

    <error-page>

        <exception-type>java.lang.NullPointerException</exception-type>

        <location>/error.jsp</location>

    </error-page>

    <error-page>

        <exception-type>javax.servlet.ServletException</exception-type>

        <location>/error.jsp</location>

    </error-page>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics