aspose file tools*
The moose likes Servlets and the fly likes Referring web page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Referring web page" Watch "Referring web page" New topic
Author

Referring web page

Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
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!


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
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

Joined: Jan 07, 2002
Posts: 89
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

Joined: Feb 20, 2001
Posts: 580
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.


James Carman, President<br />Carman Consulting, Inc.
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referring web page
 
Similar Threads
How to get response content of a website
Read HTML/Source of external site page
Integrating Tomcat With Apache
getting page contents n servlet hit
sendRedirect method