JSP 中的页面对象有什么用?能否举个例子。
JSP 允许你使用页面属性为每个 JSP 指定**错误页**。每当页面引发异常,JSP 容器就会自动调用错误页。
以下是如何为**main.jsp**指定错误页的示例。要设置一个错误页,使用**<%@ page errorPage = "xxx" %>**指令。
<%@ page errorPage = "ShowError.jsp" %>
<html>
<head>
<title>Error Handling Example</title>
</head>
<body>
<%
// Throw an exception to invoke the error page
int x = 1;
if (x == 1) {
throw new RuntimeException("Error condition!!!");
}
%>
</body>
</html>我们现在将编写一个错误处理 JSP ShowError.jsp,内容如下。请注意,错误处理页包括指令**<%@ page isErrorPage = "true" %>**。此指令会导致 JSP 编译器生成异常实例变量。
<%@ page isErrorPage = "true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<pre><% exception.printStackTrace(response.getWriter()); %></pre>
</body>
</html>访问**main.jsp**,你会收到类似以下的输出 −
java.lang.RuntimeException: Error condition!!! ...... Opps... Sorry, an error occurred. Here is the exception stack trace:
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP