exception对象(了解)
exception01.jsp
本例子没有使用exception,弹出页面用户使用户不知所措,用户体验差!
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>exception01.jsp</title>
</head>
<body>
<%
int a = 1;
int b = 0;
out.print(a/b);
%>
</body>
</html>
运行结果:
使用exception后的例子:
exception01.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.*" %>
<%--设置错误要转向的页面--%>
<%@ page 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>exception01.jsp</title>
</head>
<body>
<%
int a = 1;
int b = 0;
out.println(a/b);
%>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.*" %>
<%--是否为错误页面,默认为false--%>
<%@ page 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>
<%
if(exception != null){
out.println("程序错误信息:");
out.println(exception.getMessage());
}
%>
</body>
</html>
运行结果: