可以在web.xml文件中为整个Web应用配置错误处理页面。使用这种方法还可以根据异常类型的不同或HTTP错误码的不同配置错误处理页面。
在应用部署描述文件中(web.xml)配置页面需要使用<error-page>元素,它的子元素有<exception-type>,<err-code>和<location>,它们分别指定处理错误的异常类型、HTTP错误码和错误处理页面。前面的两个元素不能同时出现。
代码如下
web.xml中加入
notFound.jsp页面的头部关键代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" isErrorPage="true"pageEncoding="UTF-8"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<body>
</body>
在jsp中,也可以为单个页面配置单个页面错误处理信息。只要在出错页面加入小脚本信息即可
<%@ page errorPage="errorHandler.jsp"%>
在处理页面中设置属性 errorHandler.jsp
<% page isErrorPage="true" %>