1,使用JSP方式

如果配置是Jsp时,需要把isErrorPage设置为true,

以及设置 <%@ page language="Java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" isErrorPage="true"%>

错误页面:

<%@page import="java.io.PrintStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@ include file="WEB-INF/views/includes/tags.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>500 服务器内部错误</title>
</head>
<body>
<div class="ui-alert-panel">
<h1>服务器内部错误</h1>
<p>处理您的请求时发生错误!请确认您通过正确途径操作。</p>
</div>
<div style="display:none;">
<% //此处输出异常信息
exception.printStackTrace(); ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(ostr));
out.print(ostr);
%>
</div>
</body>
</html>

errorPage.jsp

可以使用这个页面来触发上面的那个错误:

<!--
errorPage: 填入一个jsp文件, 当出现错误时, 会将错误对象传递至错误页面
-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="errorPage.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String a = null;
a.toString();//制造一个错误
%>
</body>
</html>

home.jsp

2,也可以使用配置xml的方式:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>day06_JSPDemo1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--
对于项目的异常状态处理
-->
<!-- 默认的错误处理页面 -->
<error-page>
<error-code>403</error-code>
<location>/403.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 -->
<!-- 这样配置也是可以的,表示发生500错误的时候,转到500.jsp页面处理。 -->
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page> <!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/500.jsp</location>
</error-page> <error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<!--
当error-code和exception-type都配置时,exception-type配置的页面优先级高
及出现500错误,发生异常Exception时会跳转到500.jsp
-->
</web-app>

web.xml

错误页面也可以自己写,我们也可以引入公益404页面,比如:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网页不见了~</title>
<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/hybrid/app/404/search_children.js" charset="utf-8" homePageUrl="http://localhost/demo1.jsp" homePageName="回到主页"></script>
</head>
<body>
<%
//这个设置可以欺骗IE浏览器,让IE浏览器成功显示公益404页面
response.setStatus(200);
%>
</body>
</html>

500.jsp

原文链接:web.xml配置错误页面

【jsp】配置错误页面的更多相关文章

  1. JavaWeb配置错误页面

    我们在实际开发过程中经常会遇到程序出错的各种情况,比如最常见的404错误,500错误等等相关错误,服务器默认会将错误的信息直接显示在浏览器的页面上,如下图所示: 不管是谁如果看到这种情况的话,顿时就会 ...

  2. web.xml配置错误页面,及输出错误信息

    1.需要在web.xml中配置相关信息 <!-- 默认的错误处理页面 --> <error-page> <error-code>403</error-code ...

  3. .net三步配置错误页面,让你的站点远离不和谐的页面

    假设你的站点出现一堆让人看不懂的报错,那么你就不是一个合格的程序猿.也不是一个合格的站长. 以下的方面能够帮助你的站点远离让人头大的页面. 第一步:配置web.config 打开web.config, ...

  4. springboot系列六、springboot配置错误页面及全局异常

    一.spring1.x中处理方式 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new ...

  5. nginx配置错误页面

    有时候页面会遇到404页面找不到错误,或者是500.502这种服务端错误,这时候我们可能希望自己定制返回页面,不希望看到默认的或者是内部的错误页面,可以通过nginx配置来实现. 1 50x错误对于5 ...

  6. Servlet 异常处理( 配置错误页面)

    当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类型相匹配的配置. 您必须在 web.xml 中使用 error-p ...

  7. 配置Jsp错误页面

    配置Jsp错误页面一般我们有2种做法: (1)在页面中用指令进行配置,即page指令的errorPage和isErrorPage:可以使用page指令的errorPage来指定错误页!在当前JSP页面 ...

  8. web.xml配置详解之欢迎页面和错误页面

    <!-- 应用的欢迎页面  采用list的方式罗列欢迎页面, 系统会从第一个找到最后一个,找到了文件就不继续往下找了.优先显示前边的. --> <welcome-file-list& ...

  9. 配置WEB错误页面

    项目运行时,难免会出现错误,这些错误我们不可以也不方便直接让用户看到,所以配置错误页面是非常必要的. 一下是项目的Web.xml文件,在最下方阴影部分是配置错误界面. <?xml version ...

随机推荐

  1. eclipse插件大全

    http://www.cnblogs.com/homezzm/archive/2009/11/27/1612054.html

  2. oracle 复制表结构表数据

    create table Uc_t_Department3 as (select * from Uc_t_Department where 1=2);insert into Uc_t_Departme ...

  3. ArcGIS读取dem格式数据

    DEM是GIS常用的一种数据,用来做各种分析.展示等,十分有用!它实质上就是一个栅格,只不过这个栅格值表示高程,常用的格式是tif,grid等.今天听到了另外一种说法:*.dem是最常见到的DEM的格 ...

  4. JavaScript Array 对象常用方法

    <script type="text/javascript"> //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var ar ...

  5. Asp.Net下通过切换CSS换皮肤

    直接重写Render事件 protected override void Render(System.Web.UI.HtmlTextWriter writer) { StringWriter sw = ...

  6. RPi Desktop盒子安装与服务配置

    批量安装配置盒子时候,可以先安装一个,其余的从这台copy过去. 之前的部分shell记录在本地,记录如下,以免忘记.下次可直接cp执行即可: Step1, 创建用户/组 sudo groupadd ...

  7. C#中使用RabbitMQ收发队列消息

    一.程序使用NetCore.引入Nuget: Install-Package RabbitMQ.Client -Version 4.1.3 二.消息发部端: using RabbitMQ.Client ...

  8. Go语言类型转换库【github.com/demdxx/gocast】的用法

    一.导入库: go get github.com/demdxx/gocast 二.测试代码: // main.go package main import ( "fmt" &quo ...

  9. IDEA引入外部jar包的方法

    在做发短信的功能(阿里大于)的时候,我只是吧jar包拷贝到了项目的external library. 拷贝进来之后我用junit写了test方法,可以完美发送短信到手机,但是我这是个web项目,然后我 ...

  10. hadoop multipleoutputs

    http://grepalex.com/2013/05/20/multipleoutputs-part1/ http://grepalex.com/2013/07/16/multipleoutputs ...