I have used the code "request.getHeader("Referer");" The code seemed to work fine. But recently I found out that it is not working in IE. IE was throwing null. I am now left clueless about how to go forward. Is there any alternative for "Referer" header which can get the previous link and work correctly in all the browsers? Setting a custom header from the previous link is not a viable option for me. So someone please help me out on this. Thanks.
-
2The tag jsp suggests that you are trying to access headers in jsp files. You should not do that. Java processing belongs in a controller, not in a view component. – Sean Patrick Floyd Aug 23 '10 at 10:03
-
Well what should be the best way of approach in this case? I need to add the last login time of the user. – Ebbu Abraham Aug 23 '10 at 10:19
2 Answers
The "Referer" header entry is optional. You cannot rely on it being present. There is no cross-browser way to get the previous link because this depends on the user settings and proxy configuration (i.e. what the system administrators think they should allow you to see).
You must find a way to live without this information.

- 321,842
- 108
- 597
- 820
-
If thats the case, is there any way for me to get the previous link in IE? – Ebbu Abraham Aug 23 '10 at 09:17
-
4I don't think that's IE-specific. Most browsers let you deactivate the refer(r)er header, and if you type an url into the location bar manually, there won't be a referer either. You must not depend on referer information. – Sean Patrick Floyd Aug 23 '10 at 10:01
It's unclear what you need it for, but I suspect that you need it to be able to go back to some "initial page" at the same website when some action is finished. Your best option is then to pass the request URI around as request parameter. E.g. a login link:
<a href="/login?from=${pageContext.request.requestURI}">login</a>
In the login form, retain it for the next request as hidden input value of the form:
<input type="hidden" name="from" value="${param.from}">
In the login action method, just redirect to that URL after finishing the action.
response.sendRedirect(request.getParameter("from"));
If this isn't what you're looking for, then you should really elaborate your question more to ask how to achieve a functional requirement rather than asking how to achieve a (wrong) solution.
Relying any business logic flow on the referer was really been a bad idea from the beginning on. Your first webdeveloper lesson should have been: the enduser has full control over what s/he sends with the HTTP request. You shouldn't rely on all that information being present, let alone 100% correct.

- 1,082,665
- 372
- 3,610
- 3,555