提及Java Web工程中web.xml文件无人不知,无人不识,呵呵呵:系统首页、servlet、filter、listener和设置session过期时限,张口就来,可是你见过该文件中的error-page标签吗?下面直接以例子的形式说明error-page标签的使用:
一个servlet文件:
package com.ghj.packageofservlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 故意发生异常
*
* @author GaoHuanjie
*/
public class ExceptionServlet extends HttpServlet {
private static final long serialVersionUID = -8602055287059392677L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object object = null;
System.out.println(object.toString());
}
}
一个web.xml文件:
<?xml version="1.0" encoding="UTF-8"?></web-app>
一个404页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
isErrorPage="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>404页面</title>
<style type="text/css">
a:link {
color: #555555;
text-decoration: none
}
a:visited {
color: #555555;
text-decoration: none
}
a:active {
color: #555555;
text-decoration: none
}
a:hover {
color: #6f9822;
text-decoration: none
}
.text {
font-size: 12px;
color: #555555;
font-family: "";
text-decoration: none
}
</style>
</head>
<body>
<table height="100%" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tbody>
<tr>
<td valign="middle" align="center">
<table cellSpacing="0" cellPadding="0" width="500" align="center" border="0">
<tr>
<td width="17" height="17"><img height="17" src="images/co_01.gif" width="17"></td>
<td width="316" background="images/bg01.gif"></td>
<td width="17" height="17"><img height="17" src="images/co_02.gif" width="17"></td>
</tr>
<tr>
<td background="images/bg02.gif"></td>
<td>
<table class="text" cellSpacing="0" cellPadding="10" width="100%" align="center" border="0">
<tr>
<td>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td><img height="66" src="images/404error.gif" width="400"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="text" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td>
<p>
<strong><font color="#ba1c1c">HTTP404错误:</font></strong>
没有找到您要访问的页面,请与管理员联系。
</p>
<div align="right">管理员QQ:845129726 </div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td background="images/bg03.gif"></td>
</tr>
<tr>
<td width="17" height="17"><img height="17" src="images/co_03.gif" width="17"></td>
<td background="images/bg04.gif" height="17"></td>
<td width="17" height="17"><img height="17" src="images/co_04.gif" width="17"></td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
一个处理异常页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>异常页面</title>
</head>
<body>
<table cellSpacing="0" width="600" align="center" border="0" cellpadding="0" style="margin-top: 18%">
<tbody>
<tr>
<td valign="top" align="center"><img height="100" src="images\exception.png" width="100" border="0"></td>
<td>
<font style="font-size: 10pt;color: #842b00;">HTTP错误 505:系统出现异常,暂停服务。</font>
</td>
</tr>
</tbody>
</table>
</body>
</html>
工程说明:
本工程用于演示web.xml文件中error-page标签的使用
本工程编码方式:UTF-8
演示说明:
①、http://localhost:8080/test/index.jsp ——>演示404页面
②、http://localhost:8080/test/ExceptionServlet ——>演示异常页面
③、注意上面红底处的代码
④、如果把上面两个页面(一个404页面和一个处理异常页面)的代码很简洁(比如body标签中就一句30或40个字符的话),在IE浏览器中进行上面访问,你会发现页面显示的是IE浏览器自身的“报错”页面,怎样使用自己的网页呢,最好的办法是在含有红底处代码的前提下增加页面的大小!
【下载源码】
关于Java Web工程中web.xml文件,布布扣,bubuko.com
原文地址:http://blog.csdn.net/gaohuanjie/article/details/28859503