Information Systems

jsp request referrer

To get the url referrer from an HttpServletRequest use the following.

String referrer = request.getHeader("referer");

Note that referer is misspelled in the header.

In general HttpServletRequest is a little odd for getting the types of query string information that is normally needed, especially for logging applications. To get a url that doesn't include the domain part, something like the following is needed.

String referrer = request.getHeader("referer");
if (referrer == null)
referrer = "";
String url = request.getQueryString();
if (url == null)
url = "";
url = "?" + url;
url = request.getRequestURI() + url;