Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referring web page

 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Can someone explain to me how I can get the referring host name from a request in a Servlet?
For example, if I'm browsing at www.yahoo.com and I enter a URL that points to my servlet, how can I find out that I came to my servlet from the Yahoo page?
Thanks!
 
Hartmut Ludwig
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most browsers send the Referrer-information in the Request-Header. So your servlet can easyly resolve this information with a single line:
out.println(request.getHeader("Referer"));
Note that it is spelled "Referer" not "Referrer" like one would expect. This is due to a mistake of the original HTTP-Autors.
sl
Hartmut
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmmm....that's what I thought. However, when I print out all of the headers the Referer header is not included. I have also tried exactly what you suggested and it does not work...that is, I get a null value returned from request.getHeader("Referer"). (Note that I also tried "referer" with no success)
I am using Tomcat 4 for my servlet engine....could it have something to do with the way Tomcat converts the incoming HTTP request into an HttpServletRequest object?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Wisard:
Hello,
Can someone explain to me how I can get the referring host name from a request in a Servlet?
For example, if I'm browsing at www.yahoo.com and I enter a URL that points to my servlet, how can I find out that I came to my servlet from the Yahoo page?
Thanks!

Check out the information at...
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
regarding the referer header. In your case, the browser is not supposed to send the referer as www.yahoo.com. It is supposed to send nothing. Yahoo didn't refer you to the page. You simply typed the page's address and went there. If there were a link at Yahoo which took you to your page, the browser would send the referer header.
 
Hartmut Ludwig
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Wisard:
[QB]Hmmmmm....that's what I thought. However, when I print out all of the headers the Referer header is not included. I have also tried exactly what you suggested and it does not work...[QB]

I don't think so. Did you call the Servlet using a html-link or did you just type the URL into the adress-line of your browser? If so then there is really no referrer and your servlet is absolutely right to say "null"
sl
Hartmut
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic