jsp errorpage - 例外?

use*_*920 2 jsp

 <%@ page isErrorPage = "true"%>
    <body>
    <h2>Your application has generated an error</h2>
    <h3>Please check for the error given below</h3>
    <b>Exception:</b><br> 
    <font color="red"><%= exception.toString() %></font>
    </body>
Run Code Online (Sandbox Code Playgroud)

我想在JSP Expression中知道exception.toString() - 异常对象用于什么?

我们可以选择使用<%= exception.getMessage()%>吗?

谢谢..

ree*_*esy 5

我想你问的是异常变量中包含的内容.

exception是一个JSP隐式变量

exception变量包含在前一个JSP页面上抛出的任何Exception,其中一个errorPage指令转发到带有isErrorPage指令的页面.

例如

如果你有一个抛出异常的JSP(index.jsp)(我故意通过解析一个String抛出一个NumberFormatException,显然你不会写一个执行此操作的页面,它只是一个例子)

<%@ page errorPage="error.jsp" %>
<% Integer.parseInt("foo"); //throws an exception %>
Run Code Online (Sandbox Code Playgroud)

这将转发到error.jsp,

如果是error.jsp

<%@ page isErrorPage = "true"%>
<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br> 
<font color="red"><%= exception.toString() %></font>
</body>
Run Code Online (Sandbox Code Playgroud)

因为它有

<%@ page isErrorPage = "true"%>
Run Code Online (Sandbox Code Playgroud)

在page指令中,隐式变量exception将包含在前一个jsp中抛出的Exception

因此,当您请求index.jsp时,将抛出异常,并转发到error.jsp,这将输出像这样的html

<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br> 
<font color="red">java.lang.NumberFormatException: For input string: "foo"</font>
</body>
Run Code Online (Sandbox Code Playgroud)

正如@JB Nizet提到的异常是一个Throwable调用的实例exception.getMessage() For input string: "foo"而不是java.lang.NumberFormatException: For input string: "foo"