Mab
Mab

Reputation: 181

spring mvc tracing the referrer page

In annotation based spring controller. if a user was on url.com/first/page and clicked a link or submitted a form pointing to say url.com/second/page .

How to make the second/page know the url of /first/page so that the second/page can

1) redirect the user to the first/page again when the form values are processed.

2) or show a back button link to the /first/page?

Edit 1 --

request.getHeader('Referer') is another but those I think are browser based on the mercy of browser. If the browser dont do it, we cant know. I wanted a way which is application wide. some how passing the url from one page to another

Edit 1 end --

Upvotes: 3

Views: 5836

Answers (1)

Alireza Fattahi
Alireza Fattahi

Reputation: 45553

you can use two below methods:

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

or

request.getHeader("Referer"); 

In above methods you are trusting the browser behavior and also the container which make the request object available to you. I don't know why you don't want to use this method.

Or

you can get the current page/serlvet url save it in the session and use it in the second page.

 String requestUrl=request.getRequestURL();
 session.setAttribute("pervious_page",requestUrl);

Then in the second page:

session.getAttribute("pervious_page");

Upvotes: 4

Related Questions