

XML配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>
</web-app>
执行代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<!--定制错误页面-->
<head>
<title>Title</title>
</head>
<body>
<%
int x = 1/0;
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="../img/er1.jpg" alt="500">
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="../img/er1.jpg" alt="404">
</body>
</html>
本文展示了如何在XML配置中设置错误页面来处理HTTP 404和500状态码,将错误重定向到特定的JSP文件。同时,提供了两个错误页面的示例,分别对应404和500错误,显示定制的错误信息。
785

被折叠的 条评论
为什么被折叠?



