您的位置:首页 > 运维架构 > Tomcat

tomcat8.5基础 web.xml 抛出指定异常时,转到指定的页面

2017-12-06 11:51 465 查看
礼悟:
   公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。

javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
browser:Chrome/Firefox
os:windows7 x64
ide:MyEclipse 2017


参考链接:http://www.cnblogs.com/avenxia/archive/2012/02/15/2353076.html

部分转载:

<error-page></error-page> 用来处理错误代码或异常的页面,有三个子元素:
<error-code></error-code> 指定错误代码
<exception-type></exception-type> 指定一个JAVA异常类型
<location></location> 指定在web站台内的相关资源路径

比如:
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.jsp</location>
</error-page>


工程目录结构



web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Day12</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>com.jizuiku.servlet.VerifyCodeServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/VerifyCodeServlet</url-pattern>
</servlet-mapping>

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/error/ForRuntimeException.jsp</location>
</error-page>

<error-page>
<error-code>404</error-code>
<location>/error/NotFoundError.jsp</location>
</error-page>

</web-app>


MyJsp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
int n = (1 / 0);

%>
This is my JSP page. <br>
</body>
</html>


NotFoundError.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'NotFoundError.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
访问的页面居然不存在<br />
</body>
</html>


ForRuntimeException.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'ForRuntimeException.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
请稍后访问。。。┭┮﹏┭┮<br />
</body>
</html>


访问不存在的页面



访问MyJsp.jsp



扩展

  标题:web.xml配置错误页面不起作用

  链接:http://blog.sina.com.cn/s/blog_4d1498f10100nmvs.html

  部分转载  

    但怎么也跳转不了,我检查了几遍配置文件,确实没有问题,这下开始郁闷了,

    就开始google了,但网上答案各异,实在是看的我眼花缭乱,最后换了个浏览器好了,

    那这肯定是ie的原因了,就打开ie的选项看看是不是有什么特别设置的,恰好google的时候

    一篇文章说有可能跟ie选项中的友好http错误提示有关,最后问题终于解决了,呵呵。

    在IE工具->Internet选项->高级中将显示友好http错误提示的前面的勾取消,ok了。

Tomcat,优秀的开源轻量级应用服务器,值得学习。
学习资源:博文头部的参考链接 + 清净的心地。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: