JSP_错误提示_友好页面
示例1:(涉及到请求转发)
1.jsp----------------errorPage="/error.jsp"请求转发不需要写web应用名称
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" session="false" errorPage="/error.jsp" %> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 xxxx中文 8 <% 9 int i = 1/0; 10 %> 11 </body> 12 </html>
error.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" isErrorPage="true"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 服务器出错了哦...亲~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 <%= exception.getMessage() %> 9 </body> 10 </html>
示例 2: web.xml(推荐)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <error-page> 9 <exception-type>java.lang.Throwable</exception-type> 10 <location>/error/500.jsp</location> 11 </error-page> 12 13 <error-page> 14 <error-code>404</error-code> 15 <location>/error/404.jsp</location> 16 </error-page> 17 18 <welcome-file-list> 19 <welcome-file>index.jsp</welcome-file> 20 </welcome-file-list> 21 </web-app>
纸上学来终觉浅,觉知此事需躬行