How can I get the referer URL in Spring MVC Controller?
In Spring MVC 3 you can get it from request, as @BalusC already said: public ModelAndView doSomething(final HttpServletRequest request) { final String referer = request. getHeader("referer"); ... } public ModelAndView doSomething(@RequestHeader(value = "referer", required = false) final String referer) { ... }
It's available in the HTTP referer header. You can get it in a servlet as follows: String referrer = request. getHeader("referer"); // Yes, with the legendary misspelling.
The address of the webpage where a person clicked a link that sent them to your page. The referrer is the webpage that sends visitors to your site using a link. In other words, it's the webpage that a person was on right before they landed on your page.
request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link) More information here. Regarding your question, it is simply returning 'dashboard' if found in request.referer .
It's available as HTTP request header with the name referer
(yes, with the misspelling which should have been referrer
).
String referrer = request.getHeader("referer"); // ...
Here the request
is the HttpServletRequest
which is available in Spring beans in several ways, among others by an @AutoWired
.
Please keep in mind that this is a client-controlled value which can easily be spoofed/omitted by the client.
In Spring MVC 3 you can get it from request, as @BalusC already said:
public ModelAndView doSomething(final HttpServletRequest request) { final String referer = request.getHeader("referer"); ... }
but there also exists special annotation @RequestHeader which allow to simplify your code to
public ModelAndView doSomething(@RequestHeader(value = "referer", required = false) final String referer) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With