1.指定错误出现时的跳转页,通过errorPage属性决定
2.错误页必须要有明确标识,通过isErrorPage属性决定
如果一个jsp页面出现错误,会通过errorPage指定的页面进行跳转,
被跳转的页面必须将isErrorPage设置为true!
例子:show.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp"%><%--一旦jsp页面出现错误将跳转到error.jsp页面中--%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
int i=10/0; //这里将发生异常
%>
</body>
<html>
错误处理页 error.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
response.setStatus(200);
%>
<h1>程序出现了错误</h1>
</body>
</html>
客户端发送请求:服务端将error.jsp返回给客户端,地址没有改变!
还有一种指定错误的方式:
就是在虚拟目录指定全局的错误处理,修改web.xml
全局的错误处理可以处理两种类型的错误:一种是HTTP代码的错误,如404,500,还有一种是异常的错误,如:NullPointerException
<error-page>
<error-code>500</error-code>
<location>目录/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>目录/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>目录/error.jsp</location>
</error-page>