java 如何在 JSP 错误页面中显示请求的 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14300404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show the requested URL in a JSP error page?
提问by informatik01
In the error page I would like to display the URL what the user requested.
在错误页面中,我想显示用户请求的 URL。
In my web.xml:
在我的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>
This will forward to error-404.jsp, and here is the content of that file:
这将转发到error-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>
The problem it is the request.getRequestURL()
need to be changed, but don't know the keyword for what to search.
问题是request.getRequestURL()
需要更改,但不知道要搜索的关键字。
When I start the browser for http://localhost:8080/MyStuff
then I get the following error:
当我启动浏览器时,http://localhost:8080/MyStuff
我收到以下错误:
Requested resource: http://localhost:8080/MyStuff/WEB-INF/error-404.jsp not found
Requested resource: http://localhost:8080/MyStuff/WEB-INF/error-404.jsp not found
How to solve this?
如何解决这个问题?
回答by informatik01
Here is a simple example of JSP error page that shows the error code and the URL of the requested page:
下面是一个简单的 JSP 错误页面示例,显示了错误代码和请求页面的 URL:
404.jsp:
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>
Useful reading:
有用的阅读:
- "JSP.1.4 Error Handling" section in the JavaServer Pages Specification (JSR 245).
- Handling JSP Page Errors(from the official Java EE 5 Tutorial)
- JavaServer Pages 规范 (JSR 245)中的“ JSP.1.4 错误处理”部分。
- 处理 JSP 页面错误(来自官方 Java EE 5 教程)
P.S.
The use of scriptlets inside JSP is highly discouraged. Read this post.
PS
强烈建议不要在 JSP 中使用 scriptlet。阅读这篇文章。