5

问题

这是昨天(未回答)问题(见此处)的后续行动,因为我试图找到一种替代方法。

我添加了基本的

    <error-page>  
            <error-code>404</error-code>  
            <location>/404search.jsf</location>  
    </error-page>

..给我的web.xml。我现在需要获取用户输入的 URL 以提交到我的搜索功能,但我只设法获取当前 URL(在这种情况下,... 404search.jsf)而不是用户输入的实际查询。

尝试

  • HttpServletRequest.getRequestURL返回http://www.website.com/foldername/404search.jsf
  • HttpServletRequest.getRequestURI返回/foldername/404search.jsf
  • HttpServletRequest.getQueryString什么都不返回

我希望它返回/foldername/wrong-url-the-user-entered#anchor-if-there-is-any

细节...

这个想法是获取用户输入的 URL(例如www.site.com/product/99999-product-that-cant-be-foundor www.site.com/faq/support-question-that-doesnt-exist),对其进行正则表达式以删除连字符并使用99999 product that cant be foundor运行搜索查询support question that doesnt exist

有什么建议么?

4

2 回答 2

12

<error-page>是在RequestDispatcher#forward()电话服务的掩护下。原始请求的所有详细信息都可用作请求属性,这些属性由RequestDispatcher#FORWARD_XXX常量标识的键作为键控:

作为初学者,您应该知道所有请求属性都可以通过隐式 EL 对象 #{requestScope}在 EL 中使用。

因此,总而言之,这应该在视图中执行:

<p>Unfortunately, the page you requested, #{requestScope['javax.servlet.forward.request_uri']} does not exist</p>

等效地,如果需要,这应该在 bean 中执行:

String forwardRequestURI = externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
于 2013-08-15T17:41:15.477 回答
-1

您也许可以使用“referer”(拼写错误)请求标头获取该 URL。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36

Java 用法:HttpServletRequest - 如何获取引用 URL?

于 2013-08-15T17:27:26.020 回答