Tuesday, June 2, 2009

How to get client and server IP address in JSP page

Some time we have to show the server IP address on JSP page and some time we need to store client IP address to next visit purpose.
Using very few lines of code, you can get both server side and client side (browsing) IP address.

GetIPAddress.jsp

<h3> Server Side IP Address </h3><br>
<%@page import="java.net.InetAddress;" %>
<%String ip = "";
InetAddress inetAddress = InetAddress.getLocalHost();
ip = inetAddress.getHostAddress();
out.println("Server Host Name :: "+inetAddress.getHostName());%><br>
<%out.println("Server IP Address :: "+ip);%>

<h3> Client Side IP Address </h3><br>
<%out.print( "Client IP Address :: " + request.getRemoteAddr() ); %><br>
<%out.print( "Client Name Host :: "+ request.getRemoteHost() );%><br>

9 comments:

  1. Very useful coding in very easy way. thanks. I am fan of your blog. Can I give my email address and you please send your posting every time when you will put here.

    ReplyDelete
  2. Hai,
    Thanks for Your information about the how to get client and server ip address .Its very useful to me.I found my internet ip address using this sitewww.ip-details.com.But from this I knew the different information.

    ReplyDelete
  3. this code is useful for me

    I want to get client default printer, How can I do?

    thank you.

    ReplyDelete
  4. Good Posting. Very useful code.

    ReplyDelete
  5. how to get mac address of client system and how to store it in database using jsp.

    ReplyDelete
  6. how get server ip address in java

    ReplyDelete
  7. Really sir, code is working very fine. Earlier I was thinking that it would be very complex code.

    Your code perfectly worked in my project.
    Now I am searching how to get city name from getting IP address.

    Thanks again.
    Jari,
    Helsinki, Finland

    ReplyDelete
  8. import javaQuery.j2ee.GeoLocation
    request.getHeader("VIA");
    String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
    }
    GeoLocation gl = new GeoLocation();
    gl.GetGeoLocationByIP(ipAddress);
    String country = gl.Country;

    This will give you IP address of visitor you can read thie article

    How to get visitors IP address in Java?
    http://www.javaquery.com/2011/12/how-to-get-visitors-ip-address-in-java.html

    ReplyDelete
  9. This way i am not able to get the exact client ip address. Instead i am getting the firewall address which is inbetween the client and the server.

    ReplyDelete

Please put your feedback or your any question related to this blog. Thanks.