1:
在
webapp
目录下新建错误页面的目录和页面
2在web.xml中配置相关信息
<!-- *************异常处理 begin************************************* --> <error-page> <error-code>404</error-code> <location>/jsp/error/404.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/jsp/error/500.html</location> </error-page> <!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 --> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/jsp/error/500.jsp</location> </error-page> <!-- 当error-code和exception-type都配置时,exception-type配置的页面优先级高 及出现500错误,发生异常Exception时会跳转到500.jsp --> <!-- ***************异常处理 end*********************************** -->
3:
如果配置是html时,不用另做配置
如果配置是Jsp时,需要把isErrorPage设置为true,
及<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
4获取异常信息及输出
<%@page import="java.io.PrintStream"%> <%@page import="java.io.ByteArrayOutputStream"%> <%@ page language="java" import="java.util.*" charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '500.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div class="ui-alert-panel"> <h1>服务器内部错误</h1> <p>处理您的请求时发生错误!请确认您通过正确途径操作。</p> </div> <div style="display:none;"> <% //此处输出异常信息 exception.printStackTrace(); ByteArrayOutputStream ostr = new ByteArrayOutputStream(); exception.printStackTrace(new PrintStream(ostr)); out.print(ostr); %> </div> </body> </html>
5:如果404.html和500.html内容太少的话ie会对错误页面的处理在ie来看页面大小<1024b 会被认为十分不友好,所以ie就将改页面给替换成自己的错误提示页面了,解决办法就是充实一下页面,让大小超过1024即可 。