File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Know where did the come from? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Know where did the come from?" Watch "Know where did the come from?" New topic
Author

Know where did the come from?

Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66
Hi,

What I want to do, is to know where the user came from. For example a user has found my website via Google or Yahoo.
Is there a method to get the URL of these websites?

I use this method request.get("Referer"); but it returns only the latest page the user has requested.

Thank you,

Abu Romaysa
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Azz Romaysa:

I use this method request.get("Referer"); but it returns only the latest page the user has requested.


No, it shows the page they were on when they clicked the link to the current resource. If they found your site on Google and clicked the link to it, Google would show up in the referrer variable (along with the search criteria they used to arrive at that particular google page)

Example:
Here is the referer variable from a hit to my site:



In it we can see that they came from www.google.com.
Also, becuase Google puts the search terms right in the querystring, (requests with the GET method) we can also see the keywords that the user entered to get to that particular Google page. ("servlet" and "init").


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66
Yes I'm sorry, it shows the page they were on when they clicked the link to the current resource.

Let's say that the user come from google, and has requested a number of pages in my website. Finaly he has dicided to register. How could I then know that he came from Google?

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Each request doesn't carry a full history of where a user has been and there's no way you will always know where a user came from.
Some browsers may not send the referer information.
You won't get a referer if someone types the address in manually or opens a page from their browsers bookmark section (favorite places).

What you could do is put the referer value into session when a user hits your front page.

If your server has access logging turned on, take a look at the logs.
Doing so will give you an idea of how the referer varible works.
You can also see exactly where a every user has been on your site.
Following an IP up to the first occurance will usually get you an informative referer variable.
Access logs are pretty standard and are meant to be parsed by programs (such as Webalizer) so you could write a program that finds this out by interrogating them.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

I suggest a cookie since it is lightweight. Have a one-time created cookie that is created the first time the user accesses any part of the site (assuming their first time they came from an external site such as google).

So each page would do something like "If cookie does not exist create one with last page visited". Since this would be on every page, it would have to contain some external link (although not neccessarily google).

Then if the user registers, save this information iff it matches certains patterns. For example, if it contains www.google.com, its fine to save, but some may contain garbage values or irrevelant link so you just skip those. It should work in 80-90% of the cases the user found you via a search engine. As with most things on the web, nothing is bullet proof.


My Blog: Down Home Country Coding with Scott Selikoff
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

request.get("Referer");


I couldn't find this method in ServletRequest class. I appreciate if you can tell me where I can find this method.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Maybe getHeader("referer")?


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Know where did the come from?