Handling Java to get IP as 0:0:0:0:0:0:0:1 problem

Source: Internet
Author: User
Tags get ip



0:0:0:0:0:0:0:1 is the expression of IPv6, corresponding to IPv4 equivalent to 127.0.0.1, which is the native



If the project is deployed on a native Win7 system, access is accessed via localhost,



This problem may occur when you get an IP address in Java, and the IP you get will be 0:0:0:0:0:0:0:1






If the machine is in the LAN, access using its own IP access, such as my IP is: 192.168.123.156



Visit url:http://192.168.123.156:8080/test



This time the request will go through the router forwarding, so the server gets the local LAN IP, in Java to obtain the IP is 192.168.123.156






Here are a few ways to get the IP:


public static String getIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (!checkIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    private static boolean checkIP(String ip) {
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip)
                || ip.split(".").length != 4) {
            return false;
        }
        return true;
    }


REQUEST.GETREMOTEADDR () Gets the value of 0:0:0:0:0:0:0:1 for the reason and solution



1. Change the URL of localhost to 127.0.0.1



2, recently in the web development, when the JSP page gets the server IP, encountered request.getremoteaddr () get the value of 0:0:0:0:0:0:0:1, this is why, according to Reason, Should be 127.0.0.1, why the value of this to become a IPv6, and I found that this situation only in the server and the client are on the same computer will appear (for example, when the use of localhost access), and later on the Internet check the reason, the original is/etc/ Hosts this thing mischief (should be C:\Windows\System32\drivers\etc\hosts this file on Windows), only need the file #:: 1 localhost This line is commented out to solve the problem. In addition, localhost this file is very useful, here you can add your own entries, such as adding 192.168.0.212 myweb this way, in the browser can only use 192.168.0.212来 access, and can use MyWeb to replace.



If not resolved, the local access time with 127.0.0.1 or native IP instead of localhost can be resolved

Handling Java to get IP as 0:0:0:0:0:0:0:1 problem


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.