How to Implement Global IP‑Based User Location Display in Java
This article explains how Java developers can retrieve a client’s IP address from an HttpServletRequest, handle proxy headers, and use the ip2region library (via Maven) to map the IP to its province and city, including code snippets and configuration details.
Modern mobile apps and websites often display a user’s location without distinction. To achieve this based on the client’s IP address, a Java backend must first obtain the real IP from the incoming request.
Retrieving the Client IP
The following utility method extracts the IP address from an HttpServletRequest. It checks common proxy headers ( x-forwarded-for, Proxy-Client-IP, WL-Proxy-Client-IP, HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR) and falls back to request.getRemoteAddr(). If the address is a loopback ( 127.0.0.1 or 0:0:0:0:0:0:0:1), it resolves the actual host address via InetAddress.getLocalHost(). Finally, when the header contains multiple comma‑separated IPs, the method keeps only the first one.
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for ");
if (ipAddress == null || ipAddress.length() == 0 || "unknown ".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP ");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown ".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP ");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown ".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inetAddress.getHostAddress();
}
}
if(null != ipAddress && ipAddress.length() > 15){
// "***.***.***.***".length() = 15
if(ipAddress.indexOf(",") > 0){
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}Mapping IP to Geographic Region
Once the IP is obtained, the ip2region library can translate it into province and city information. The library works with a binary .xdb file and supports two lookup modes: a file‑based query that caches a VectorIndex, or a full in‑memory load of the .xdb for the fastest lookups.
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.6.4</version>
</dependency>The article notes that large providers already offer such services, but they often impose request limits. By integrating ip2region locally, developers avoid external rate limits while achieving reliable location resolution.
Conclusion
With the IP‑extraction utility and the ip2region dependency, a Java backend can reliably display a user’s geographic location across any web or mobile application.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
