5

Problem

This is a follow-up to yesterday's (unanswered) question (see here) as I try to find an alternative approach.

I added the basic

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

..to my web.xml. I now need to get the URL the user entered to submit to my search function, but I only manage to get the current URL (in this case, ...404search.jsf) instead of the actual query the user entered.

Attempts

  • HttpServletRequest.getRequestURL returns http://www.website.com/foldername/404search.jsf
  • HttpServletRequest.getRequestURI returns /foldername/404search.jsf
  • HttpServletRequest.getQueryString returns nothing at all

I want it to return /foldername/wrong-url-the-user-entered#anchor-if-there-is-any

Details...

The idea is to get the URL the user entered (such as www.site.com/product/99999-product-that-cant-be-found or www.site.com/faq/support-question-that-doesnt-exist), REGEX it to remove the hyphens and run a search query with 99999 product that cant be found or support question that doesnt exist.

Any suggestions?

Community
  • 1
  • 1
tdtm
  • 312
  • 2
  • 12

2 Answers2

12

The <error-page> is under the covers served by a RequestDispatcher#forward() call. All details of the original request are available as request attribues which are keyed by the keys as identified by RequestDispatcher#FORWARD_XXX constants:

You as starter should know that all request attributes are in EL available via the implicit EL object #{requestScope}.

So, all with all, this should do in the view:

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

And equivalently, this should do in the bean, if necessary:

String forwardRequestURI = externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • On my custom 404 page, `#{requestScope['javax.servlet.forward.request_uri']}` didn't work (nothing displayed) but I figured out that `#{requestScope['javax.servlet.error.request_uri']}` did the job. – Mathieu Castets Jan 17 '14 at 11:19
  • @Bobby: answer assumed that page is forwarded by `FacesServlet` via `*.jsf` mapping, but you're right. – BalusC Jan 17 '14 at 11:27
-1

You might be able to get that URL with the "referer" (misspelled) request header.

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

Java usage: HttpServletRequest - how to obtain the referring URL?

Community
  • 1
  • 1
samuelgrigolato
  • 182
  • 1
  • 6