views:

60

answers:

2

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.

+4  A: 

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.

Aaron Digulla
If thats the case, is there any way for me to get the previous link in IE?
Ebbu
I 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.
seanizer
+3  A: 

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.

BalusC