舉例說(shuō)明:mustBeError.jsp
<%@ page contentType="text/html; charset=gb2312"
language="java" import="java.sql.*,javax.servlet.*,javax.servlet.http.*" errorPage="error.jsp" %>
<%
//這個(gè)頁(yè)面一定會(huì)出錯(cuò)。
int i=0;
int j=1;
out.println(j/i);
%>
在此中通過(guò)errorPage="error.jsp"來(lái)指定出錯(cuò)時(shí)錯(cuò)誤處理頁(yè)面
<--------------------------->
error.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" isErrorPage="true" import="java.io.*"%>
<html>
<head>
<title>出錯(cuò)了!</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
出錯(cuò)了!<br>
發(fā)生了以下的錯(cuò)誤:
<br><hr><font color=red><hr>
getMessage():<br>
<%=exception.getMessage()%><br><hr>
getLocalizedMessage():<br>
<%=exception.getLocalizedMessage()%><br><hr>
PrintStatckTrace():<br>
<%
StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);
exception.printStackTrace(pw);
out.println(sw);
%><br>
</font></body>
</html>
在error.jsp中,page指令中指定:isErrorPage="true"
<---------------------->
在web.xml中聲明異常和錯(cuò)誤頁(yè)面
例如:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html</location>
</error-page>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/NumberFormatException.html</location>
</error-page>
</web-app>
說(shuō)明:指定錯(cuò)誤代碼為404是,調(diào)用/pageNotFound.html
也可以指定錯(cuò)誤類型如:java.lang.NumberFormatException,出現(xiàn)此錯(cuò)誤類型是調(diào)用/NumberFormatException.html
聯(lián)系客服