permaculture playing cards
The moose likes Servlets and the fly likes Determining IP address I'm being communicated over Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Determining IP address I Watch "Determining IP address I New topic

Determining IP address I'm being communicated over

Mike Dahmus

Joined: Mar 07, 2002
Posts: 29
I have code running on the server in a login servlet - in the case which is causing concern we're using a clustering solution which results in multiple IP addresses being active (on different interfaces) for the same machine. One is the 'normal' address and one is the 'cluster'. (i.e. one machine at a time 'owns' the cluster address).

For various reasons, I need to know which IP address is being used on a given servlet call. (i.e. the machine's normal eth0 address is, and it also has an eth0:0 address of (cluster); the servlet needs to know which one is being used for the current HTTP activity so I can potentially tell them "don't log in here; log in there instead").

getServerName() works somewhat - but provides a hostname if the user had connected that way from the client. That makes me unsure that this is even going to work for the case I'm concerned about. Basically we used to do this on the client side but discovered that the NAT case doesn't work with this (where the servers are behind a NAT and thus the client can't compare what it sees in a well-known spot on the server against its own settings, since the client's versions of the addresses aren't the same).

Any suggestions?

Mike Dahmus

-----Mike Dahmus
Mike Dahmus

Joined: Mar 07, 2002
Posts: 29
followup: I now see the method getLocalAddr() was added in Servlet 2.4. Unfortunately, I'm stuck for now with Servlet 2.3 (can't afford to upgrade our embedded Tomcat to 5 at this point - way too risky). Any hacks which can get me there on 2.3?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

If you don't mind having a slightly different application on the clusters, add a servlet filter to the bad clusters.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Determining IP address I'm being communicated over
It's not a secret anymore!