javaweb:通过web.xml实现错误页面的定制

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

我们首先建立两个页面,一个是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

 

 测试结束,没有问题。

 

(本文仅作个人学习记录用,如有纰漏,敬请指正)

posted @   谁知道水烫不烫  阅读(165)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示