java web之路 jsp exception

来源:互联网 发布:录制视频软件手机 编辑:程序博客网 时间:2024/06/14 08:59

exception对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生这个对象,页面要使用这个对象,必须把isErrorPage设置为true。exception对象实际是java.lang.Throwable的对象,常用方法如下:
String getMessage() 获得返回异常的信息
String toString() 获提异常的简短描述

exception_test.jsp发生异常交给excepiton.jsp页面处理
exception_text.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    import="java.util.*,java.text.*" errorPage="exception.jsp"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>    <h1>excepiton test</h1>    <hr>    <%out.print(100/0); %></body></html>

exception.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    import="java.util.*" isErrorPage="true"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>    <h1>excepiton内置对象</h1>    获得错误信息:<%=exception.getMessage() %><br>    获得简短描述:<%=exception.toString() %><br>    错误栈信息:<%=exception.getStackTrace() %></body></html>