JSP/Servlet HTTP 404错误处理 [英] JSP / Servlet HTTP 404 error handling
问题描述
我想在我的Web应用程序中处理HTML 404错误.
I'd like to handle HTML 404 errors in my web app.
我可以这样写:
<error-page>
<error-code>404</error-code>
<location>/view/error404.jsp</location>
</error-page>
这很完美,但是我想记录用户键入的每个无效URL. 当我在error404.jsp中使用scriptlet时:
This works perfectly, but I want to log each of the invalid urls that the users type. When I use a scriptlet in the error404.jsp like this:
<%
System.out.println(request.getRequestURL());
%>
我总是得到: http://localhost:8080/webApp/view/error.jsp ,因为用户将从无效的URL转发到我的error404.jsp.
I always get : http://localhost:8080/webApp/view/error.jsp, because the users will be forwarded to my error404.jsp from the invalid URL.
我应该如何获得无效的URL?还是应该编写一个servlet来捕获其他servlet未明确处理的所有请求?
How should I get the invalid URL? Or how should I write a servlet that catch all requests that are not handled explicitly by other servlets?
推荐答案
使用键javax.servlet.forward.request_uri
将其存储为请求属性:
It's stored as request attribute with the key javax.servlet.forward.request_uri
:
<p>URL: ${requestScope['javax.servlet.forward.request_uri']}</p>
或者,如果您仍然使用已在10年前升级的旧版JSP 1.x,请执行以下操作:
Or if you're still on the legacy JSP 1.x which was already been upgraded over a decade ago, then do so:
<p>URL: <%= request.getAttribute("javax.servlet.forward.request_uri"); %></p>
这篇关于JSP/Servlet HTTP 404错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!