Custom Error Page - Get the originally requested URL

Problem

This is the answer to yesterday's (unanswered) question ( see here ), as I am trying to find an alternative approach.

I added basic

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

.. to my web.xml . Now I need to get the URL that the user entered to send my search function, but I will be able to get the current URL (in this case ... 404search.jsf ) instead of the actual request entered by the user.

Attempts

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

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

More details ...

The idea is to enter a user url (e.g. www.site.com/product/99999-product-that-cant-be-found or www.site.com/faq/support-question-that-doesnt-exist ), REGEX to remove hyphens and run a search query using 99999 product that cant be found or support question that doesnt exist .

Any suggestions?

+4
source share
2 answers

<error-page> is under the covers served by the RequestDispatcher#forward() call. All details of the original request are available as request attributes, which are entered by the keys specified by RequestDispatcher#FORWARD_XXX constants:

You, as a starter, should know that all request attributes are in the EL, accessible through the implicit EL object #{requestScope} .

So, everything with everyone, this should look in the view:

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

And, equivalently, this must be done in a bean, if necessary:

 String forwardRequestURI = externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI); 
+8
source

You may be able to get this URL with the "referer" request header (with an error).

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

Java usage: HttpServletRequest - how to get a link url?

-one
source

Source: https://habr.com/ru/post/1497289/


All Articles