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.


<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>


  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.

  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.

  3. this code is useful for me

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

    thank you.

  4. Good Posting. Very useful code.

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

  6. how get server ip address in java

  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.
    Helsinki, Finland

  8. import javaQuery.j2ee.GeoLocation
    String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
    GeoLocation gl = new GeoLocation();
    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?

  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.


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