user489041
user489041

Reputation: 28294

IP Address in Java and JSF

I am using the following code to get the IP address form a client.

public String getIp(@Context HttpServletRequest requestContext, @Context SecurityContext context) {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
    }
    return ipAddress;
}

However, when it executes, it returns 0:0:0:0:0:0:0:1. It is running on my local pc, and I would expect it to return good ol 127.0.0.1. Any ideas why not?

Upvotes: 0

Views: 3344

Answers (3)

Carlos H. Raymundo
Carlos H. Raymundo

Reputation: 61

This is late but for those who will visit this page in the future. If you are running Tomcat, you may set JAVA_OPTS environment variable and add

-Djava.net.preferIPv4Stack=true 

and

-Djava.net.preferIPv4Addresses=true

In eclipse it maybe added in:

Debug As -> Debug configuration -> Environment.

Upvotes: 1

Javier
Javier

Reputation: 12388

Your machine has dual stack (IPv4/IPv6). The 0:0:0:0:0:0:0:1 address (also written as ::1) is the IPv6 equivalent for localhost.

Upvotes: 1

Mitesh Pathak
Mitesh Pathak

Reputation: 1306

However, when it executes, it returns 0:0:0:0:0:0:0:1. It is running on my local pc, and I >would expect it to return good ol 127.0.0.1. Any ideas why not?

If the machine is behind a proxy you won't be able to get it's local IP or domain information, in any server side technology

Refer [1] Getting IP address of client

Upvotes: 1

Related Questions