在错误页面中,我想显示用户请求的 URL。
In my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>MyStuff</display-name>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error-404.jsp</location>
</error-page>
</web-app>
这将转发至错误404.jsp,这是该文件的内容:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Page Not found</title>
</head>
<body>
<p align="center">
<%
out.println("Requested resource: " + request.getRequestURL()+ " not found");
%>
</body>
</html>
问题在于request.getRequestURL()
需要更改,但不知道要搜索的关键字。
当我启动浏览器时http://localhost:8080/MyStuff
然后我收到以下错误:
Requested resource: http://localhost:8080/MyStuff/WEB-INF/error-404.jsp not found
怎么解决这个问题呢?
下面是一个简单的 JSP 错误页面示例,显示错误代码和请求页面的 URL:
404.jsp:
<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Error page</title>
<meta charset="utf-8">
</head>
<body>
<button onclick="history.back()">Back to Previous Page</button>
<h1>404 Page Not Found.</h1>
<br />
<p><b>Error code:</b> ${pageContext.errorData.statusCode}</p>
<p><b>Request URI:</b> ${pageContext.request.scheme}://${header.host}${pageContext.errorData.requestURI}</p>
<br />
</body>
</html>
有用的阅读:
- "JSP.1.4 错误处理” 部分中Java服务器页面
规格(JSR
245).
-
处理 JSP 页面
错误(来自官方 Java EE 5 教程)
P.S.
强烈建议不要在 JSP 中使用 scriptlet。读这个帖子.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)