案例:演示exception对象的使用

一、创建发生异常的页面exception_happen.jsp

在page指令中需要指定由哪个页面来处理异常(如:errorPage="do_error.jsp")

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="do_error.jsp"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>exception object test</title>
</head>
<body>
	<%
		int a = 3;
		int b = 0;
	%>
	输出结果为:<%=(a / b)%><!--此处会产生异常  -->
</body>
</html>

二、创建处理异常的页面do_error.jsp

需要在page指令中设置它能处理异常(如:isErrorPage="true"),这样它能获取到从发生异常的页面传过来的exception对象

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>error page</title>
</head>
<body>
<!--       显示异常信息 -->
     <%=exception.getMessage() %><br />
</body>
</html>

 三、访问

http://localhost:8080/chapter06/exception_happen.jsp

注意:用ie浏览器可能报500错误

解决方法:

猜你喜欢

转载自blog.csdn.net/daqi1983/article/details/120826978