1

I am using Tomcat server with Java web application. I need client public IP address from request. But i am unable to get that, i used request.getHeader("x-forwarded-for") request.getRemoteAddr() methods but I get client machine Local IP address.

yogesh
  • 463
  • 6
  • 15

2 Answers2

3

You are running your server in a local network. If its in the wild request.getRemoteAddr()should do its job.

If you really need even in a local network your public IP which could all be the same in your local network since its behind a router or something that uses NAT it dosen't make much sense in my eyes. Even though if you want to get your public IP use a service like ipecho: http://ipecho.net/plain

Zarathustra
  • 2,853
  • 4
  • 33
  • 62
0

If you are building web application in JSP & wants to send client's public IP address to the server, you can use JavaScript with JSP to achieve this.

//JavaScript dynamically created to obtain CLIENT MACHINE'S IP ADDRESS
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://www.telize.com/jsonip?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);

        //below given function returns CLIENT MACHINE'S IP ADDRESS to the text box
        function DisplayIP(response)
        {
            document.getElementById("clientip").value = response.ip;
        }

IP address will be stored in clientip & pass it to the server list you post a form & pass it to a servlet.

Smokey
  • 1,857
  • 6
  • 33
  • 63