问题描述
我正在通过web.xml中的以下命令将404错误重定向到servlet.
I'm redirecting 404 errors to a servlet via the following in my web.xml.
<error-page>
<error-code>404</error-code>
<location>/notFound.do</location>
</error-page>
我想记录请求试图去的地方,但是我没有从引荐来源标头中获取它:request.getHeader("referer")
I'd like to log where the request was trying to go, but I'm not getting it from the referrer header: request.getHeader("referer")
如果我刚刚打过任何旧的随机不存在的页面,则显示"null".
That shows 'null' if I just hit any old random non-existent page.
request.getRequestURL()/request.getRequestURI()都只显示最终的登陆servlet信息(即/notFound).
And request.getRequestURL()/request.getRequestURI() both merely shows the final landing servlet info (I.e., /notFound).
是否可以获取所请求的错误"页面网址?
Any way to get the 'bad' page URL that was requested?
推荐答案
是的,它可以作为名称为javax.servlet.forward.request_uri
的请求属性使用,该属性由 RequestDispatcher#FORWARD_REQUEST_URI
.错误页面位置即由简单的 RequestDispatcher#forward()
调用.
Yes, it's available as a request attribute with the name javax.servlet.forward.request_uri
, which is keyed by RequestDispatcher#FORWARD_REQUEST_URI
. The error page location is namely invoked by a simple RequestDispatcher#forward()
call.
因此,您可以在servlet中按以下方式获取它:
So, you can get it as follows in servlet:
String originalUri = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
或在EL中
<p>Original URI: ${requestScope['javax.servlet.forward.request_uri']}</p>
这篇关于web.xml 404重定向到servlet,如何获取原始URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!