我想在我的网络应用程序中处理 HTML 404 错误。
我可以这样写:
<error-page>
<error-code>404</error-code>
<location>/view/error404.jsp</location>
</error-page>
这工作得很好,但我想记录用户输入的每个无效网址。
当我在 error404.jsp 中使用 scriptlet 时,如下所示:
<%
System.out.println(request.getRequestURL());
%>
我总是得到:http://localhost:8080/webApp/view/error.jsp http://localhost:8080/webApp/view/error.jsp,因为用户将从无效的 URL 转发到我的 error404.jsp。
我该如何获取无效的 URL?或者我应该如何编写一个 servlet 来捕获其他 servlet 未显式处理的所有请求?
它与键一起存储为请求属性javax.servlet.forward.request_uri
:
<p>URL: ${requestScope['javax.servlet.forward.request_uri']}</p>
或者,如果您仍在使用十多年前就已升级的旧版 JSP 1.x,那么请执行以下操作:
<p>URL: <%= request.getAttribute("javax.servlet.forward.request_uri"); %></p>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)