用到两个jsp页面指令:
isErrorPage=true/false ( true 则页面可以使用exception,false不可用)
errorPage 默认忽略,例子: errorPage="error/error.jsp"
代码如下:
error.jsp 异常处理页:
<%@page contentType="text/html;charset=gbk" isErrorPage="true"%> <HTML> <HEAD> <link rel="stylesheet" type="text/css" href="/style/sound.css"> </HEAD> <body bgcolor=gray topmargin="10" leftmargin="0" id="TheBody"> <table border="0" bgcolor=#e0e0e0 align="center" cellspacing="0" style="BORDER-RIGHT:gray 2px solid; BORDER-TOP:white 2px solid; BORDER-LEFT:white 2px solid; BORDER-BOTTOM:gray 2px solid" width="407"> <tr> <td width=10px></td> <td><%=exception.getMessage()%></td> </tr> </table> </body> </HTML>
我们来测试一下.制造一个异常:
testerr.jsp
<%@ page contentType="text/html;charset=GBK"%> <%@ page errorPage="error.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <!--s 没有定义,所以会出错--> <%=s%> </body> </html>