exception对象用来处理JSP文件执行时发生的所有错误和异常,只有在page指令中设置isErrorPage="true"的页面中才能使用。exception对象的常用方法如表6.7所示
下面用一个简单例子来说明使用:
index.jsp
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
errorPage="error.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>起始页</title>
</head>
<body>
<%
float price = Float.parseFloat("10元");
%>
</body>
</html>
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" 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>错误提示页面</title>
</head>
<body>
<%=exception.getMessage()%><br>
<%=exception.getLocalizedMessage()%><br>
<%=exception.toString()%><br>
<%=exception.fillInStackTrace()%><br>
</body>
</html>