views:

187

answers:

1

Hi all,

I want to improve my site usability by providing users some kind of "related search" funcionality if they are coming from a search engine.

It is possible, from a servlet, to obtain if a visitor of my site is coming from search engine results page?

If yes, is possible to obtain the query string?

Thanks in advance

EDIT: Thanks Thillo, this is my first approach using Referer header. I'm not very proud about it. All comments and suggestions about this code will be appreciated.

+5  A: 

That would be the (peculiarly spelled) Referer-Header.

request.getHeader("Referer");

You need to parse it and decide if it is a search engine URL. Every search engine has a different way of specifying the query, for example http://www.google.com/search?q=test.

Thilo
Thanks a lot, take a look at this code http://pastebin.com/f1fcaad0b
SourceRebels
That code looks fine. I don't think that there is a fundamentally different better way to do it. When matching host names, you may want to include a "." in the pattern, or even do a postfix match (google.com, bing.com), to avoid matching "notyahoo.com" (even though it is not a big problem if you matched those anyway, plus those hostnames are really unlikely).
Thilo