举例说明:mustBeError.jsp
- <%@ page contentType="text/html; charset=gb2312"
- language="java" import="java.sql.*,javax.servlet.*,javax.servlet.http.*" errorPage="error.jsp" %>
- <%
- //这个页面一定会出错。
- int i=0;
- int j=1;
- out.println(j/i);
- %>
在此中通过errorPage="error.jsp"来指定出错时错误处理页面
---------------------------
error.jsp
- <%@ page contentType="text/html; charset=gb2312" language="java" isErrorPage="true" import="java.io.*"%>
- <html>
- <head>
- <title>出错了!</title>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- </head>
- <body>
- 出错了!<br>
- 发生了以下的错误:
- <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中声明异常和错误页面
例如:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <web-app xmlns=""
- xmlns:xsi=""
- xsi:schemaLocation=" 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>
说明:指定错误代码为404是,调用/pageNotFound.html
也可以指定错误类型如:java.lang.NumberFormatException,出现此错误类型是调用/NumberFormatException.html
阅读(5252) | 评论(0) | 转发(0) |