JSP中errorPage的问题

           当在JSP的page指令中设置errorPage="url"就可以设置处理异常事件的JSP文件。如可以这样写:

                <%@ page errorPage="error.jsp" %>

           这样的话,当页面中出现异常的时候,就会自动跳转到错误处理页面。但是要注意,

           只有当正常打开页面,然后在页面中出现异常的时候才会跳转到错误处理页面,如果页面本身有语法错误而出现了编译错误,是不会跳转的!

           例:index.jsp


<%@ page contentType="text/html; charset=GB2312" language="java" errorPage="error.jsp"%>
<html><head><title>lifecycle</title></head><body>
<%!
private int initVar = 0;
private int serviceVar = 0;
private int destroyVar = 0;
public void jspInit() { initVar ++; }
public void jspDestroy() { destroyVar ++; }
%>
<%
serviceVar ++;
String content1="初始化次数:" + initVar;
String content2="响应客户请求次数:" + serviceVar;
String content3="销毁次数:" + destroyVar;
out.println("<h1>hahaha</h1>");
               if(serviceVar==5){
                    String info = getServletInfo();
                    throw new Exception("Exception in:" + info);
               }
%>
<h1><%=content1%></h1><h1><%=content2%></h1><h1><%=content3%></h1>
<!--this is some funny words.-->
</body></html>

下面的是error.jsp

<%@ page contentType="text/html; charset=GB2312" language="java" isErrorPage="true"%>
<html><head><title>error</title></head><body>
this is the error page.
</body></html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值