12.exception对象

excepton对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生了这个对象,如果一个jsp页面要应用此对象,就必须把isErrorPage设置为true,否则无法编译。它实际上是java.lang.Throwable的对象,常用方法:

String getMessage()返回描述异常的消息

String toString()反水关于异常的简短描述信息

void printStackTrace()显示异常及其栈轨迹

Throwable FillInStackTrace()重写异常的执行栈轨迹

使用:在实际中有时候会出现异常,可以专门写一个jsp来处理

抛出异常的jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <h1>抛出异常页面</h1>
    <%
    System.out.println(100/0);
    %>
</body>
</html>

需要在page头那里添加处理异常的jsp,即errorPage="exception.jsp"

exception.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=utf-8" isErrorPage="true"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <h1>exception内置对象</h1>
    异常消息为:<%=exception.getMessage() %><br>
    异常的字符串描述:<%=exception.toString() %>
</body>
</html>

需要在头部添加

isErrorPage="true"

运行的结果

 





posted @ 2016-08-16 15:32  笨蛋敏  阅读(322)  评论(0编辑  收藏  举报