web.xml 配置404和500错误的自定义页面

JAVA学习网 2017-09-07 11:50:01

web.xml文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>项目名称</display-name>
<welcome-file-list>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>

<error-page>
  <error-code>500</error-code>
  <location>/500.html</location>
 </error-page>
</web-app>

配置好

<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>

然后对404.html添加自己喜欢的样式。

 

也可以配置JSP页面,配置JSP页面的关键在于

1.  isErrorPage="true"

2.  response.setStatus(HttpServletResponse.SC_OK);

404.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%response.setStatus(HttpServletResponse.SC_OK); %>

<%
/**
* 本页面是在客户查找的页面无法找到的情况下调用
*/
response.setStatus(HttpServletResponse.SC_OK);
 %>
<body>
正在加载... <a href="javascript:history.go(-1)">返回</a>
<br/>
呜···可能页面连接更改了,请按 F5 键刷新整个页面看看!

</body>

 

 



 

阅读(785) 评论(0)