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").
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.
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.
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>