Tomcat处理故意错误页面404社死现场

Tomcat 8.0以上

部署前端项目或者后端项目都可

不论是前端项目也好后段项目也罢,我们难免会使用到Tomcat作为服务中间件进行部署项目的情况,虽然Tom猫的时代已经慢慢脱离我们日常开发技术的范围了,但是偶尔还是会用到,这里就简单做一下自己的笔记心得,能用到的童鞋们可以收藏一下试试。

解压一个空的tomcat进行启动正常访问Tom猫试试

localhost:8080

在这里插入图片描述

场景复现

输入一个不存在的请求路径试试

localhost:8080/admin

在这里插入图片描述

我们主要解决这个丑陋的页面改成我们想要指定的页面。

其实在内部发生了错误tomcat会请求一个/error404.html的请求

一、制定自己的页面

<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>404-对不起!您访问的页面不存在</title>
		<style type="text/css">
			.head404 {
				width: 580px;
				height: 234px;
				margin: 150px auto 0 auto;
				background: url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat;
			}
			.txtbg404 {
				width: 499px;
				height: 169px;
				margin: 10px auto 0 auto;
				background: url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;
			}
			.txtbg404 .txtbox {
				width: 390px;
				position: relative;
				top: 30px;
				left: 60px;
				color: #eee;
				font-size: 13px;
			}
			.txtbg404 .txtbox p {
				margin: 5px 0;
				line-height: 18px;
			}
			.txtbg404 .txtbox .paddingbox {
				padding-top: 15px;
			}
			.txtbg404 .txtbox p a {
				color: #eee;
				text-decoration: none;
			}
			.txtbg404 .txtbox p a:hover {
				color: #FC9D1D;
				text-decoration: underline;
			}
		</style>
	</head>
	<body bgcolor="#494949">
		<div class="head404"></div>
		<div class="txtbg404">
			<div class="txtbox">
				<p>对不起,您请求的页面不存在、或已被删除、或暂时不可用</p>
				<p class="paddingbox">请点击以下链接继续浏览网页</p>
				<p><a style="cursor:pointer" οnclick="history.back()">返回上一页面</a></p>
				<p><a href="https://www.daixiaorui.com">返回网站首页</a></p>
			</div>
		</div>
	</body>
</html>
</html>

二、修改配置

找到tomcat目录下的conf目录下的web.xml文件,添加内容:

<error-page>
    <error-code>404</error-code> <!--错误码-->
    <location>/error404.html</location> <!--相对webapps/ROOT/-->
</error-page>
<error-page>
    <error-code>403</error-code> <!--错误码-->
    <location>/error404.html</location> <!--相对webapps/ROOT/-->
</error-page>
<error-page>
    <error-code>500</error-code> <!--错误码-->
    <location>/error404.html</location> <!--相对webapps/ROOT/-->
</error-page>

三、将页面放到指定位置

针对整个tomcat发生的所有(包括项目)404等错误信息都可以将404页面放在 ROOT/目录下

针对单个项目发生的404等错误信息 ,将404页面放在webapps/项目/下即可

## 在项目中配置
<error-page> 
    <error-code>404</error-code> <!--错误码-->
    <location>/WEB-INF/page/error404.html</location> <!--相对maven项目webapp文件夹-->
</error-page> 

四、验证

浏览器输入

localhost:8080/admin

在这里插入图片描述

再输入自己正常资源路径的时候正确放行,按理自己可指定403/405等错误代码的指定页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值