关于Java Webproject中web.xml文件
提及Java Webproject中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 version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/exception.jsp</location>
</error-page>
<servlet>
<servlet-name>ExceptionServlet</servlet-name>
<servlet-class>com.ghj.packageofservlet.ExceptionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExceptionServlet</servlet-name>
<url-pattern>/ExceptionServlet</url-pattern>
</servlet-mapping>
</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>
project说明:
本project用于演示web.xml文件里error-page标签的使用
本project编码方式:UTF-8
演示说明:
①、http://localhost:8080/test/index.jsp ——>演示404页面
②、http://localhost:8080/test/ExceptionServlet ——>演示异常页面
③、注意上面红底处的代码
④、假设把上面两个页面(一个404页面和一个处理异常页面)的代码非常简洁(比方body标签中就一句30或40个字符的话),在IE浏览器中进行上面訪问。你会发现页面显示的是IE浏览器自身的“报错”页面,如何使用自己的网页呢,最好的办法是在含有红底处代码的前提下添加页面的大小!
【下载源代码】