0

I have a Spring (Java) website that uses Kemp Technologies' LoadMaster as the balancer, which sits before my web servers.

I am able to get the right IP address of a visitor at any page via:

request.getRemoteAddr()

However, I am not able to get the correct domain of the referrer. Here is what I did:

  1. I have the following code in a HandlerInterceptorAdapter, which is called for each web request. The code is to check whether a particular session variable exists. If yes, no action. If no, record the referrer's domain in session.

     HttpSession session = request.getSession();
     String value = (String) session.getAttribute("referrer");
     if (value == null) {
         String scheme = request.getScheme();
         String serverName = request.getServerName();
         int portNumber = request.getServerPort();
         String url = scheme +"://"+serverName+ (portNumber == 80? "": portNumber);
        session.setAttribute("referrer", url);
     }
    
  2. In other pages when I need the referrer's domain, I have the following:

     String value = (String) request.getSession().getAttribute("referrer"); 
    

The problem is that the value I got is always my own domain (ex. http://www.example.com).

Where have I gone wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
curious1
  • 14,155
  • 37
  • 130
  • 231
  • I think it should be `request.getHeader()`, not a session attribute. Also, the first 'R' is capitalized. – markspace Sep 24 '16 at 17:10
  • Thanks so much for your input! Without storing request.getHeader() in the session when a visitor comes to my website for the first time, can I get correct value from request.getHeader() when the visitor views other pages? By uppercase "R", you mean request.getHeader("Referer") ? – curious1 Sep 24 '16 at 17:37
  • Yup, didn't try it, but reading the spec it looks to me like you'll need to use "Referer" on the `request` parameter. – markspace Sep 24 '16 at 18:51
  • Hi thanks fore the follow-up. Did tests. Two things. It is "referrer". Please see this post, which is correct based on my test. Second, the value has to be stored in session. Later values of the header will be my own website. Thanks for helping me. If you make your comment a post with my tests, I will select it as the answer. Regards. – curious1 Sep 24 '16 at 18:53
  • I'm really shocked that "Referer" did not work but "referer" did. I'd resolve to keep a close eye on that, something is wonky there, but I can't argue with working. – markspace Sep 24 '16 at 18:57
  • Please see this SO post. Forgot to include it in my earlier comment. http://stackoverflow.com/questions/2648984/httpservletrequest-how-to-obtain-the-referring-url – curious1 Sep 24 '16 at 19:05

0 Answers0