Press enter to see results or esc to cancel.


Get IP address Location of Client/User JAVA web project

This project shows how to take IP address of user in your Web Project. And you can use the IP address to predict the location where they are using.

  1. Update the Key after registering in the website https://ipinfodb.com/
    Get_Location_From_IP.java

    
    package Modal;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    public class Get_Location_From_IP {
        public Location_Use_Bean get_ip_Details(String ip) {
            String key = "9d6jinufcfdfacc213c7ddf4ef911dfe97b55e4696be3732bf8302876c09ebad06b";
            ip = ip.trim();
            Location_Use_Bean obj_Location_Use_Bean = new Location_Use_Bean();
            System.out.println("The ip adress is before " + ip + "  split");
            try {
                if (ip.contains(",")) {
                    String temp_ip[] = ip.split(",");
                    ip = temp_ip[1].trim();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            System.out.println("The ip adress is after " + ip + " split");
            URL url;
            try {
                url = new URL("http://api.ipinfodb.com/v3/ip-city/?key=" + key + "&ip=" + ip);
                BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                String strTemp = "";
                String temporaray = "";
                String temp_array[] = null;
                while (null != (strTemp = br.readLine())) {
                    temporaray = strTemp;
                }
                temp_array = temporaray.split(";");
                System.out.println("Str length is " + temp_array.length);
                int length = temp_array.length;
                
                if (length == 11) {
                    obj_Location_Use_Bean.setIp_address(ip);
                    if (temp_array[3] != null) {
                        obj_Location_Use_Bean.setCountry_code(temp_array[3]);
                    }
                    if (temp_array[4] != null) {
                        obj_Location_Use_Bean.setCountry(temp_array[4]);
                    }
                    if (temp_array[5] != null) {
                        obj_Location_Use_Bean.setState(temp_array[5]);
                    }
                    if (temp_array[6] != null) {
                        obj_Location_Use_Bean.setCity(temp_array[6]);
                    }
                    if (temp_array[7] != null) {
                        obj_Location_Use_Bean.setZip(temp_array[7]);
                    }
                    if (temp_array[8] != null) {
                        obj_Location_Use_Bean.setLat(temp_array[8]);
                    }
                    if (temp_array[9] != null) {
                        obj_Location_Use_Bean.setLon(temp_array[9]);
                    }
                    if (temp_array[10] != null) {
                        obj_Location_Use_Bean.setUtc_offset(temp_array[10]);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return obj_Location_Use_Bean;
        }
        public static void main(String[] args) {
            Get_Location_From_IP obj_Get_Location_From_IP = new Get_Location_From_IP();
            Location_Use_Bean obj_Location_Use_Bean = obj_Get_Location_From_IP.get_ip_Details("49.213.63.255");
            System.out.println("IP Address--" + obj_Location_Use_Bean.getIp_address());
            System.out.println("Country Code-- " + obj_Location_Use_Bean.getIp_address());
            System.out.println("Country--" + obj_Location_Use_Bean.getCountry());
            System.out.println("State--" + obj_Location_Use_Bean.getState());
            System.out.println("City--" + obj_Location_Use_Bean.getCity());
            System.out.println("ZIP--" + obj_Location_Use_Bean.getZip());
            System.out.println("Lat--" + obj_Location_Use_Bean.getLat());
            System.out.println("Lon--" + obj_Location_Use_Bean.getLon());
            System.out.println("Offset--" + obj_Location_Use_Bean.getUtc_offset());
        }
    }
    
    
    
  2. Console Output
    The ip adress is before 49.213.63.255  split
    The ip adress is after 49.213.63.255 split
    Str length is 11
    IP Address--49.213.63.255
    Country Code-- 49.213.63.255
    Country--India
    State--Gujarat
    City--Ahmedabad
    ZIP--380028
    Lat--23.0333
    Lon--72.6167
    Offset--+05:30
    
  3. Location_Use_Bean.java
    
    package Modal;
    public class Location_Use_Bean {
        private String country_code;
        private String utc_offset;
        private String country;
        private String state;
        private String city;
        private String zip;
        private String lat;
        private String lon;
        private String ip_address;
        public String getIp_address() {
            return ip_address;
        }
        public void setIp_address(String ip_address) {
            this.ip_address = ip_address;
        }
        public String getCountry_code() {
            return country_code;
        }
        public void setCountry_code(String country_code) {
            this.country_code = country_code;
        }
        public String getUtc_offset() {
            return utc_offset;
        }
        public void setUtc_offset(String utc_offset) {
            this.utc_offset = utc_offset;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }
        public String getState() {
            return state;
        }
        public void setState(String state) {
            this.state = state;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getZip() {
            return zip;
        }
        public void setZip(String zip) {
            this.zip = zip;
        }
        public String getLat() {
            return lat;
        }
        public void setLat(String lat) {
            this.lat = lat;
        }
        public String getLon() {
            return lon;
        }
        public void setLon(String lon) {
            this.lon = lon;
        }
    }
  4. index.jsp
    
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@page import="java.text.SimpleDateFormat"%>
    <%@page import="java.util.Calendar"%>
    <%@page import="java.util.TimeZone"%>
    <%@page import="java.util.Date"%>
    <%@page import="Modal.Get_Location_From_IP"%>
    <%@page import="Modal.Location_Use_Bean"%><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    String ip = request.getHeader("x-forwarded-for");
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getRemoteAddr();
    }
    Get_Location_From_IP obj_Get_Location_From_IP=new Get_Location_From_IP();
    Location_Use_Bean obj_Location_Use_Bean=obj_Get_Location_From_IP.get_ip_Details(ip);
    %>
    Location details of User<br>
    1--IP Address--<%=ip %><br>
    2--Country Code--<%=obj_Location_Use_Bean.getCountry_code() %><br>
    3--Country--<%=obj_Location_Use_Bean.getCountry() %><br>
    4--State--<%=obj_Location_Use_Bean.getState() %><br>
    5--City--<%=obj_Location_Use_Bean.getCity() %><br>
    6--ZIP--<%=obj_Location_Use_Bean.getZip() %><br>
    7--Lat--<%=obj_Location_Use_Bean.getLat() %><br>
    8--Lon--<%=obj_Location_Use_Bean.getLon() %><br>
    9--Offset--<%=obj_Location_Use_Bean.getUtc_offset() %><br>
    </body>
    </html>
    
  5. If you try this from your localhost, It will get error because the IP address which your project will be reading as 0:0:0:0:0:0:0:1 or 127.0.0.1
  6. If you host this in your website, The project will take the public IP of the user and will get the Location of user correctly.

Download the project here.

GetIpAddressOfClient.rar

Comments

Leave a Comment