提及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. 腾讯云ubuntu搭建tomcat

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6377945.html 一:工具准备 Putty+Xftp5,见上一篇博文:http://www.cnblogs ...

  2. new malloc

    https://blog.csdn.net/happyxieqiang/article/details/50775847 0.       属性 new/delete是C++关键字,需要编译器支持.m ...

  3. Spring Cloud Dalston.SR5 BUG一记

    使用Dalston.SR5版本的Zuul时, 发现Ribbon重试不能切换服务实例, 换成Edgware.SR3,同样的配置可以切换实例进行重试 还有个不升级所有Spring Cloud组件的方法,仅 ...

  4. Docker运行python容器

    容器是镜像运行的实例,而镜像保存在仓库里,测试或者发布生产环境只需要pull下来即可,相对传统的应用部署,能很好的保持环境的一致,节省运维时间.最近公司内部的java和.net服务也已经全部容器化,实 ...

  5. codeM 2018 资格赛

    比赛链接:https://www.nowcoder.com/activity/2018codem/index?from=meituan 1.下单 给定若干商品,可以选择打折.满减两种方式. #incl ...

  6. Android百度地图相关内容汇总

    Android百度地图知识讲解 1.百度地图开发环境搭建    http://www.apkbus.com/android-116050-1-1.html 2.Android百度地图系列教程    h ...

  7. Eclipse中10个最有用的快捷键组合(转)

    Eclipse中10个最有用的快捷键组合   一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.   ...

  8. 解决flume运行中的一个异常问题!

    今天在本地测试flume的exec  监控文件   分割的问题!!!遇到各种141异常问题! 怀疑是在切割文件的时候超过了监控文本的时间,导致flume异常退出,,,所以增加了keep-alive 时 ...

  9. du命令解决linux磁盘空间满的问题(很不错的哦)

    首先你要确定是不是真正的是因为数据空间占满磁盘,经常是因为某个程序的日志占满了空间.当发现磁盘满了以后不要着急,使用以下命令从根目录开始排除查找哪个文件夹最大: du --max-depth=1 找到 ...

  10. 进阶之路(基础篇) - 010 Arduino 函数(基本、串口、SPI)

    一.基本函数 pinMode(引脚号,模式); digitalWrite(引脚号,电平状态);          //默认低电平(或浮空) digitalRead(数字输入端口号); analogRe ...