在web.xml中配置全局的异常处理界面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>


<!-- 给异常的jsp页面配置对应类型的异常错误界面,这样提高用户访问的友好度 -->
<error-page>
	<exception-type>java.lang.ArithmeticException</exception-type>
	<location>/errors/error.jsp</location>
</error-page>
<!-- 根据可能出现的异常代码配置相应的错误显示界面,将会大大提高用户的访问友好度,尽可能完整地配置异常界面 -->
<error-page>
	<error-code>404</error-code>
	<location>/errors/404.jsp</location>
</error-page>
<error-page>
	<error-code>500</error-code>
	<location>/errors/500.jsp</location>
</error-page>
</web-app>

如果发生错误的JSP页面显式指定了errorPage="/errors/erros.jsp"属性,那么会覆盖web.xml中的异常配置;

处理错误的jsp页面,如果显式指定了isErrorPage="true",那么错误页面将会传递exception异常对象,否则不会传递:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	String message = exception.getMessage();
 %>
</body>
</html>


你可能感兴趣的:(异常处理)