31

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 reach the final jsp that is used to render the page, the ${pageContext.request.requestURL} call in that jsp returns the path of this jsp.

For example

  1. Original request: /booking/getCruiseDetails
  2. gets forwarded to: /booking/validateCruiseDeteails.jsp
  3. gets forwarded to: /booking/validateUser.jsp
  4. finally gets forwarded to: /booking/showCruiseDetails.jsp

So, in /booking/showCruiseDetails.jsp when I call ${pageContext.request.requestURL} I get /booking/showCruiseDetails.jsp

How do you get the the original (client made) request url from a jsp that has been reached through multiple forwards. I did find the following posts on stackoverflow that hint at the solution here and here, but they don't address how you would go about finding the original request url after multiple forwards have occurred.

Community
  • 1
  • 1
Salman Paracha
  • 1,697
  • 2
  • 16
  • 27

6 Answers6

59

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

Community
  • 1
  • 1
Lenny Markus
  • 3,398
  • 2
  • 24
  • 30
  • 1
    This is actually a better answer than the accepted one. The servlet specification guarantees that this propertly survives multiple forwards: "Note that these attributes must always reflect the information in the original request even under the situation that multiple forwards and subsequent includes are called." (Servlet specification, 9.4.2: Forwarded Request Parameters) – LordOfThePigs Oct 16 '15 at 07:47
  • Hey thanks, But how can i get the parameters it only prints the request – shareef Nov 09 '18 at 09:56
12

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

lukastymo
  • 26,145
  • 14
  • 53
  • 66
3
${requestScope["javax.servlet.forward.request_uri"]}

or with single quotes

${requestScope['javax.servlet.forward.request_uri']}
Marcus
  • 1,857
  • 4
  • 22
  • 44
2

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

request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI)
fingerprints
  • 2,751
  • 1
  • 25
  • 45
1

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

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>
Víctor
  • 493
  • 5
  • 7