i'm trying to retrieve ip address from HttpServletRequest object using java (Spring mvc).
Few of my systems are connected in lan with different ip address. i'm able to host my web app (using apache tomcat 7) in one system and access the web app in other system's.
i tried using below code to retrieve the IP address from the http request header by triggering url from browser from different systems.
but i can see only the value 127.0.0.1 or localhost:8080
request.getHeader("X-FORWARDED-FOR"); // returns null
request.getRemoteAddr(); // returns 127.0.0.1
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
logger.info("1key: "+key+" 1value: "+value);
}
//(along with other values, i get) ip value as localhost:8080
i think the ip shown by the above code, is the ip of the system where the app is launched.
Lets say there are 5 systems
system 1 - ip: 172.22.16.1 (HOST system)
system 2 - ip: 172.22.16.2
system 3 - ip: 172.22.16.3
system 4 - ip: 172.22.16.4
system 5 - ip: 172.22.16.5
when i access the web app from system 5, i need to capture the ip address of system 5 which is 172.22.16.5. But in my case i get only 127.0.0.1
Please let me know what is the problem here and why i'm not able to see the IP address from which request is originating and what should i do to get the output i require.
Update
Though i didnot solve my underlying problem, i was able to figure out what was causing the problem (browse through comments). The discussion helped