提及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&nbsp;</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浏览器自身的“报错”页面,如何使用自己的网页呢,最好的办法是在含有红底处代码的前提下添加页面的大小!

下载源代码

关于Java Webproject中web.xml文件的更多相关文章

  1. 谈谈对XML的理解?说明Web应用中Web.xml文件的作用?

    谈谈对XML的理解?说明Web应用中Web.xml文件的作用? 解答:XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard ...

  2. Java Web中web.xml文件简单介绍

    参考博客: https://www.cnblogs.com/Y-oung/p/8401549.html 1.XML 声明和根元素 <?xml version="1.0" en ...

  3. web应用中web.xml文件的解释

    一.web.xml配置文件常用元素及其意义预览 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></di ...

  4. 关于java项目中的XML文件

    一,xml的机制 1.xml文件会在服务器启动的时候进行加载 2.加载完成后根据xml文件里面配置的属性对集成的对象进行属性和行为赋予 3.xml会有很多不同的标签,每个标签都有它特定的含义 二.为什 ...

  5. struts1.x中web.xml文件的配置

    1.配置欢迎文件清单      当客户访问Web应用时,如果仅仅给出Web应用的Root URL,没有指定具体的文件名.Web容器会自动调用Web应用的欢迎文件.<welcome-file-li ...

  6. Java是如何解析xml文件的(DOM)

    Java解析xml文件 在Java程序中读取xml文件的过程也称为"解析xml文件": 解析的目的: 获取 节点名和节点值 获取 属性名.属性值. 四中解析方式: DOM SAX ...

  7. Java Web的web.xml文件作用及基本配置(转)

    其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...

  8. web.xml文件中的web-app元素 部署

    [转载]web.xml文件中的web-app元素 (2012-05-24 13:35:57) 转载▼ 标签: 转载 分类: java 挺全 的 呵呵呵 转了 原文地址:web.xml文件中的web-a ...

  9. web.xml文件中加载顺序的优先级

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

随机推荐

  1. ReactNative调用aar文件(附:如何打开、查看aar文件内容)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7275897.html ReactNative可以用来进行一些嵌入式设备的操作终端开发,比如:ATM机.自动售卖 ...

  2. MySQL中 PK NN UQ BIN UN ZF AI 的意思

    PK   Belongs to primary key作为主键 NN   Not Null非空 UQ  Unique index不能重复 BIN  Is binary column存放二进制数据的列 ...

  3. ORA-01950: no privileges on tablespace xxx ORA-01950: 对表空间 'xxx'无权限

    场景: 创建用户,在用户scheme下新建了一张表,插入数据时报错 ORA-01950: 对表空间 'xxx'无权限 创建用户 /*第1步:创建临时表空间 */ create temporary ta ...

  4. dubbo注册中心介绍

    作者:微子Lee链接:https://www.jianshu.com/p/2f4cfb6ed048 Dubbo的注册中心有好多种,包括Multicast.Zookeeper.Redis.Simple等 ...

  5. java if语句

    //if语句 //Test10.java import java.util.Scanner; public class Test16{ public static void main(String[] ...

  6. adjustResize和adjustPan的比较

    在下面的描述中,编辑框的maxLine都设定为10. 在信息列表界面中,编辑框在RelativeLayout中定义.编辑框上边(above)的列表组件的高度不会缩小为0,导致显示出现一点问题. 当信息 ...

  7. android 用代码画虚线边框背景

                   1.虚线画效果,可以使用Android中的xml来做. 2.直接上代码: <RelativeLayout android:id="@+id/coupon_ ...

  8. jsp路径问题之base

    <base href="<%=basePath%>"> base 标记用于指定页面中所有相对路径的基点, 而默认的相对路径的基点是页面所在路径. 测试代码如 ...

  9. mysql 常用命令,连接数据库,查看建表语句,批量导入数据,批量更新数据,连接查询

    1. 1)MySQL 连接本地数据库,从cmd中进入mysql命令编辑器: root root分别为用户名和密码 mysql -uroot -proot 2)MySQL 连接本地数据库,用户名为“ro ...

  10. vim缩进参考线

    编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...