常见的错误页面类型有很多,这里我们以404和500为例,404是找不到需要访问的资源,而502是服务器的配置错误。
我们首先建立两个页面,一个是404.jsp,一个是500.jsp

404.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
<img alt="" src="/error/images/404.png" width="800" height="600">
</body>
</html>
500.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500</title>
</head>
<body>
<img alt="" src="/error/images/500.png" width="800" height="600">
</body>
</html>
再写一个测试500的页面
500test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500test</title>
</head>
<body>
<% int i = 1/0;%>
</body>
</html>
配置web.xml文件
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
进行测试
首先我们访问一个不存在的页面测试404

然后我们访问500test.jsp测试500

测试结束,没有问题。
(本文仅作个人学习记录用,如有纰漏,敬请指正)
原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/277515.html