码迷,mamicode.com
首页 > Web开发 > 详细

获取来自jsp errorpage 的exception 对象

时间:2015-11-09 12:34:43      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

1. 在web.xml 中配置(全局)

  <error-page>
  <exception-type>javax.servlet.ServletException</exception-type>
  <location>/error/logJspExceptionToFile.do</location>
  </error-page>

或在jsp 页面中配置 (单页面)

<%@ page errorPage="/error/logJspExceptionToFile.do" %>

 

2.在servlet 中处理传过来的异常.

@WebServlet("/error/logJspExceptiontoFile.do")
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";
        }
    }
}

 

获取来自jsp errorpage 的exception 对象

标签:

原文地址:http://www.cnblogs.com/predisw/p/4949484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!