提及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. postgresql修改配置生效方法

    对于配置服务器,,太多时候我们在Linux中做的操作是,配置*.conf文件,然后重启服务.而很多服务都具有reload功能,而但是具体到某个配置,有时候直接说出需不需要重启服务而使得配置生效,这并不 ...

  2. PgSQL · 源码分析· pg_dump分析

    PostgreSQL本身提供了逻辑导出工具pg_dumpall和pg_dump,其中pg_dumpall导出所有的数据库,pg_dump导出单个数据库,两个工具的用法和参数不再详细介绍,本文从代码层面 ...

  3. 【PMP】三点估算法

    通过考虑估算中的不确定性和风险,可以提高持续时间估算的准确性. 最可能时间(tM):基于最可能获得的资源.最可能取得的资源生产率.对资源可用时间的现实预计,资源对其他参与者的可能依赖关系及可能发生的各 ...

  4. 如何快速的得到string的最后一个字符

    str='python' print str[-1]

  5. JavaScript的NaN-唯一 一个自己不等于自己的对象!!

    JavaScript的NaN为什么不等于NaN 在JS中 Object === Object 感觉没有任何问题 这两个都代表的一个东西 但是如果你试过 NaN === NaN 是返回false为什么呢 ...

  6. 基于微信小程序的票价和时间选择以及计算总价

    发布时间:2018-10-30   技术:wxml+wxss+JS   概述 微信小程序实现选择时间和票价,根据选择的票价和时间实时计算总价,当时间和票价都显示缺货状态时,点击弹出缺货登记,需要选择票 ...

  7. LUA pcall 多个返回值

    You call lua_pcall with the number of arguments you are passing and the number of results you want. ...

  8. 【RS】Stochastic PCA with ℓ2 and ℓ1 Regularization - ℓ2 和 ℓ1正则的随机 PCA

    [论文标题] Stochastic PCA with ℓ2 and ℓ1 Regularization   (ICML 2018) [论文作者]—Poorya Mianjy  (Johns Hopki ...

  9. Geocoding java调用百度地图API v2.0 图文 实例( 解决102错误)

    如何使用? 第一步:申请ak(即获取密钥),若无百度账号则首先需要注册百度账号. 第二步,拼写发送http请求的url,注意需使用第一步申请的ak. 第三步,接收http请求返回的数据(支持json和 ...

  10. magento 为用户注册增加一个字段(转)

    步骤 I. 加一个occupation/title字段到用户注册页,差不多在register.html的54行,在email下方加一个Occupation显示代码 代码: <li>< ...