Workaround for GETLOCALADDR () NULL in Apache + Tomcat load Balancing mode

Source: Internet
Author: User

In the Apache + Tomcat load Balancing mode, through the REQUEST.GETLOCALADDR () can not get the server-side IP address, the return value is null problem, looked under, Baidu did not have the valuable answer, to the official website to find the next, said is:
The AJP protocol only passes the Web server host name and port. It does not pass the IP address. Therefore, Getlocalname () would return whatever is passed via AJP but getlocaladdr () would always return null.
Whilst Getlocaladdr () could be modified to return the IP address Tomcat are listening on for AJP connections, I don ' t like The inconsistency that would generate when compared to Getlocalname () and Getlocalport ().
Because the AJP conversion lets the Java side not obtain the server's IP address, you can use the following methods to obtain the server-side IP address:

Java code
    1. String Currentip = Inetaddress.getbyname (Request.getservername ()). Gethostaddress ();
String Currentip = Inetaddress.getbyname (Request.getservername ()). Gethostaddress ();

This is always the IP that gets the load server
Access to the domain name is first obtained through Request.getservername () and then resolved to the IP address via inetaddress.getbyname (String serverName), but the format is the form of the domain/IP. The IP address is then obtained via gethostaddress ().
In fact, if you need to resolve to IP through the domain name, you can also use this function to obtain:

Java code
    1. Inetaddress.getbyname (domain name). gethostaddress ()
Inetaddress.getbyname (domain name). gethostaddress ()

Workaround for GETLOCALADDR () NULL in Apache + Tomcat load Balancing mode

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.