Custom error page - get originally requested URL

后端 未结 2 2008
说谎
说谎 2020-12-20 23:47

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

相关标签:
2条回答
  • 2020-12-21 00:29

    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:

    • FORWARD_CONTEXT_PATH: "javax.servlet.forward.context_path"
    • FORWARD_PATH_INFO: "javax.servlet.forward.path_info"
    • FORWARD_QUERY_STRING: "javax.servlet.forward.query_string"
    • FORWARD_REQUEST_URI: "javax.servlet.forward.request_uri"
    • FORWARD_SERVLET_PATH: "javax.servlet.forward.servlet_path"

    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);
    
    0 讨论(0)
  • 2020-12-21 00:32

    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?

    0 讨论(0)
提交回复
热议问题