web.xml 404重定向到servlet,如何获取原始URI? [英] web.xml 404 redirect to servlet, how to get the original URI?

查看:195
本文介绍了web.xml 404重定向到servlet,如何获取原始URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过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的请求属性使用,该属性由

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆