作者:yutuo 发布:2012-01-15 15:22 分类:
Java 阅读:
47 views
抢沙发
在之前,我一般用Try…Catch…来处理Servlet的异常。最近在看孙鑫老师写的《Java Web开发详解——XML XSLT Servlet JSP深入剖析实例应用》,发现还可以通过web.xml来配置Servlet的异常处理。觉得这个方法还是最好的。
主要是通过配置WEB-INF下web.xml的error-page来达到我们的异常处理。error-page下有三个标签error-code,exception-type,location。其中location是必需的,用于指定异常处理画面;error-code,exception-type在一个error-page有且只能有一个,用于指定异常或者错误码。
如果处理画面是Servlet,我们可以通过service来处理异常。其中request参数属性里,能取到javax.servlet.error.status_code, javax.servlet.error.exception_type, javax.servlet.error.message, javax.servlet.error.exception, javax.servlet.error.request_uri, javax.servlet.error.servlet_name这六个值,具体的说明和类型,请参考测试代码。
比如,测试用的web.xml配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
|
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <error-page> <error-code>404</error-code> <location>/ErrorPage</location> </error-page> <error-page> <error-code>500</error-code> <location>/ErrorPage</location> </error-page> <error-page> <exception-type>java.io.IOException</exception-type> <location>/ErrorPage</location> </error-page> </web-app>
|
测试用的Servlet如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
|
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ErrorPage */ @WebServlet("/ErrorPage") public class ErrorPage extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // HTTP协议的状态码 Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code"); out.println(String.format("status_code: %s", statusCode)); // 未捕获的异常类名 Class<?> exceptionType = (Class<?>)request.getAttribute("javax.servlet.error.exception_type"); out.println(String.format("exception_type: %s", exceptionType)); // 错误发生画面response.sendError设置的消息 // 或者未捕获的异常的消息 String message = (String)request.getAttribute("javax.servlet.error.message"); out.println(String.format("message: %s", message)); // 未捕获的异常 Throwable throwable = (Throwable)request.getAttribute("javax.servlet.error.exception"); out.println(String.format("Throwable: %s", throwable)); // 当前请求URI String requestUri = (String)request.getAttribute("javax.servlet.error.request_uri"); out.println(String.format("request_uri: %s", requestUri)); // 错误画面的Servlet String servletName = (String)request.getAttribute("javax.servlet.error.servlet_name"); out.println(String.format("servlet_name: %s", servletName)); } }
|
测试1:当提求画面不存在时,结果如下:
1 2 3 4 5 6
|
|
status_code: 404 exception_type: null message: /JavaWeb/HelloWorld1 Throwable: null request_uri: /JavaWeb/HelloWorld1 servlet_name: default
|
测试2:当提求画面发生IOException(web.xml配置)时,结果如下:
1 2 3 4 5 6
|
|
status_code: 500 exception_type: class java.io.IOException message: Test Error Msg Throwable: java.io.IOException: Test Error Msg request_uri: /JavaWeb/HelloWorld servlet_name: net.yutuo.javaweb.servlet.HelloWorld
|
测试3:当提求画面发生其它异常时,结果如下:
1 2 3 4 5 6
|
|
status_code: 500 exception_type: null message: Throwable: javax.servlet.ServletException: Test Error Msg request_uri: /JavaWeb/HelloWorldInput servlet_name: net.yutuo.javaweb.servlet.HelloWorldInput
|
根据以上的信息,写一个自己的异常画面应该是学过一点Java的就没问题了,呵呵。
参考资料:孙鑫《Java Web开发详解——XML XSLT Servlet JSP深入剖析实例应用》第10章 Servlet的异常处理机制
本文固定链接: http://yutuo.net/archives/571ef5e8ea91be55.html | 宇托的狗窝