我有一个 JSP,其中包含使用如下代码的其他 JSP:
<jsp:include page="include.jsp" />
我只是希望能够捕获异常并向最终用户显示错误消息(如果缺少 include.jsp)。如何检测或捕获缺失的资源状况?
最佳答案
我认为JSP有隐式对象,其中之一就是Exception。
教程点示例:
<%@ 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 FileNotFoundException("Error, one file is missing!!!");
}
%>
</body>
并且只有您处理了错误页面中的异常:
<%@ 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>
关于java - 如何捕获缺少 JSP 文件的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495649/