自定义错误页面 - 获取最初请求的URL

时间:2013-08-15 17:21:17

标签: jsf servlets http-status-code-404

问题

这是对昨天(未答复)问题(see here)的跟进,因为我试图找到另一种方法。

我添加了基本的

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

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

尝试

  • 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

详细...

我们的想法是获取用户输入的网址(例如www.site.com/product/99999-product-that-cant-be-foundwww.site.com/faq/support-question-that-doesnt-exist),将其删除以删除连字符并使用99999 product that cant be found或{{1}运行搜索查询}}

有什么建议吗?

2 个答案:

答案 0 :(得分:11)

<error-page>RequestDispatcher#forward()电话提供服务。原始请求的所有详细信息均可作为请求属性提供,这些属性由RequestDispatcher#FORWARD_XXX常量标识的密钥键入:

作为初学者应该知道所有请求属性都在implicit EL object #{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);

答案 1 :(得分:-1)

您可以使用“referer”(拼写错误的)请求标头获取该网址。

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

Java用法:HttpServletRequest - how to obtain the referring URL?

相关问题