異常處理

1、web.xml
  i.error-code:錯(cuò)誤碼 
  <error-page>  
      <error-code>404</error-code> 
      <location>/404.jsp</location>
  </error-page>
  
  ii.exception-type
當(dāng)servlet執(zhí)行時(shí),可能產(chǎn)生許多異常。而當(dāng)異常產(chǎn)生時(shí),Web容器將產(chǎn)生一個(gè)包含A Servlet Has Occurred消息的缺省頁(yè)。但是,用戶也可返回一個(gè)容器,該容器應(yīng)包含為給定異常指定的錯(cuò)誤頁(yè)。
  <error-page>  
      <exception-type>org.sprngside.bookstore.UserNotFound</exception-type>  
      <location>/userNotFound.jsp</location>
  </error-page>
  
  
2、jsp
 <@ errorPage="error.jsp">


3、Spring MVC里的異常控制
   spring-mvc可在xxx-serverlet.xml里定義default和 按Excepiton類型影射的錯(cuò)誤頁(yè)面,
   和Servlet規(guī)范比,主要作了Spring特色的JSP路徑轉(zhuǎn)向和日志記錄.參見(jiàn)bookstore-servlet.xml
  
 <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      <property name="defaultErrorView" value="/error.jsp"/>
      <property name="exceptionMappings">
           <props>               
                <prop key="org.springside.framework.base.BusinessException">/businessError.jsp</prop>
            </props>       
       </property>   
 </bean>

 

=========================================
  error.jsp會(huì)同時(shí)處理jsp,servlet,和spring拋過(guò)來(lái)的異常
  
  jsp的異常在exception 變量中.(要指定<%@ page isErrorPage="true" %>)
  
  spring的異常在(Exception) request.getAttribute("exception")
  
  servlet的異常在(Exception)request.getAttribute("javax.servlet.error.exception")
  
  使用 (String) request.getAttribute("javax.servlet.error.request_uri")獲得 request_uri
  使用 logger.error(exception.getMessage(), exception); 記錄整個(gè)異常棧

   
  · javax.servlet.error.exception:實(shí)際的異常擲出的Throwable對(duì)象
  · javax.servlet.error.request_uri:導(dǎo)致問(wèn)題產(chǎn)生的資源的URI字符串對(duì)象
  · javax.servlet.error.status_code:錯(cuò)誤類型的整形值
  · javax.servlet.error.exception_type:產(chǎn)生錯(cuò)誤的異常類的實(shí)例
  · javax.servlet.error.message:異常信息字符串
  
  


error.jsp 完整的代碼

<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ taglib prefix="c" uri=" <html>
<head>
    <title>Error Page</title>
    <script src="<c:url value="/scripts/prototype.js"/>" type="text/javascript"></script>
    <script language="javascript">
        function showDetail()
        {
            $('detail_error_msg').toggle();
        }
    </script>
</head>

<body>

<div id="content">
    <%
        //Exception from JSP didn't log yet ,should log it here.
        String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
        LogFactory.getLog(requestUri).error(exception.getMessage(), exception);
    %>

    <h3>System Runtime Error: <br><%=exception.getMessage()%></h3><br>

    <button onclick="history.back();">Back</button><br>

    <p><a href="#" onclick="showDetail();">Administrator click here to get the detail.</a></p>

    <div id="detail_error_msg" style="display:none">
        <pre><%exception.printStackTrace(new java.io.PrintWriter(out));%></pre>
    </div>
</div>
</body>
</html>