How to get the original request url from a servlet/jsp after multiple servlet forwards

前端 未结 6 734

I am working on a cruise booking app using struts/tiles that uses multiple internal servlet/jsp forwards to reach the right jsp for display. But, once you r

相关标签:
6条回答
  • 2020-12-29 03:32

    I found a better answer in this post [ How do you detect the URL in a Java Servlet when forwarding to JSP? ]

    On the target JSP use:

    request.getAttribute("javax.servlet.forward.request_uri")

    To find out what the original URL was.

    It doesn't require you to take any extra steps on the forwarding servlet

    0 讨论(0)
  • 2020-12-29 03:35

    You can show it without using a bean reference with the following:

    <h:outputText value="#{requestScope['javax.servlet.forward.request_uri']}" />
    

    Of course, you need to map the 404 page in your web.xml file though.

    <error-page>
        <error-code>404</error-code>
        <location>/xhtml/pg/error/404.xhtml</location>
    </error-page>
    
    0 讨论(0)
  • 2020-12-29 03:41

    Consider using servlet filters instead to validate information. This means you can avoid your validation forwarding and just stay in a single JSP file.

    0 讨论(0)
  • 2020-12-29 03:43
    ${requestScope["javax.servlet.forward.request_uri"]}
    

    or with single quotes

    ${requestScope['javax.servlet.forward.request_uri']}
    
    0 讨论(0)
  • 2020-12-29 03:50

    You can use a filter to putting origin address to request attribute and then read it from jsp

    Filter mapped to /booking/* execute:

    request.setAttribute("origin", request.getRequestURL());
    

    Jsp:

    ${pageContext.request.attribute["origin"]}
    

    This works because filter has set REQUEST dispatcher by default. It means filter executes only for direct client requests not for forwarding/including

    0 讨论(0)
  • 2020-12-29 03:50

    Same as @Lenny Markus but using the provided constant in the Request Dispatcher class.

    request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI)
    
    0 讨论(0)
提交回复
热议问题