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>
Tuesday, June 2, 2009
Subscribe to:
Post Comments (Atom)
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.
ReplyDeleteHai,
ReplyDeleteThanks 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.
this code is useful for me
ReplyDeleteI want to get client default printer, How can I do?
thank you.
Good Posting. Very useful code.
ReplyDeletehow to get mac address of client system and how to store it in database using jsp.
ReplyDeletehow get server ip address in java
ReplyDeleteReally sir, code is working very fine. Earlier I was thinking that it would be very complex code.
ReplyDeleteYour code perfectly worked in my project.
Now I am searching how to get city name from getting IP address.
Thanks again.
Jari,
Helsinki, Finland
import javaQuery.j2ee.GeoLocation
ReplyDeleterequest.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
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