Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 874
Salman Paracha Avatar asked Mar 27 '11 15:03

Salman Paracha


People also ask

What is the default HTTP method in the servlet?

The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, and so on). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on.


2 Answers

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

like image 139
Lenny Markus Avatar answered Sep 20 '22 09:09

Lenny Markus


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

like image 33
lukastymo Avatar answered Sep 18 '22 09:09

lukastymo