If you are like me you probably have already run into the problem of
getting the remote IP Address from the HttpServletRequest on the VISTA
operating system. Here is the method I was using to check if the
client (browser) was running on the server.
String s1 = HttpServletRequest.getRemoteAddr();
String s2 = HttpServletRequest.getLocalAddr();
If (s1.equalsIgnoreCase(s2)) {
//running on local machine
}
The above code works fine iff :
a) You are not running on VISTA
b) You are running on VISTA and your server does not have dual IP
stack (IPv4 and IPv6)
c) You have VISTA and a dual IP stack but you use the actual IPv4 IP
Address in the URL instead of type http://localhost
d) You have a dual IP stack and you have disabled IPv6 using one of
multiple methods available
The reason is that when both IPv6 and IPv4 are enabled on VISTA, any
call to localhost returns the IPV6 format of the address , while the
getLocalAddress() method returns the IP address of the server is IPv4
format. Camparing the two will there always fail.
For a more detailed explanation of the reason why this problem arises,
see
http://en.wikipedia.org/wiki/IPv6#Special_addresses
http://java.sun.com/j2se/1.5.0/docs/guide/net/ipv6_guide/index.html
The correct way to check IP Addresses that works universally is to
construct an java.net.InetAddress object from the remote address and
compare it according as shown in method below:
public static boolean isLocalClient(HttpServletRequest request) {
HttpServletRequest l_request = request;
try {
InetAddress l_remote = InetAddress.getByName
(l_request.getRemoteAddr());
if (l_remote.isLoopbackAddress()) {
return true;
}
InetAddress l_local_host = InetAddress.getLocalHost();
String ls_localaddress = l_local_host.getHostAddress();
String ls_remote_address = l_remote.getHostAddress();
return (ls_remote_address != null &&
ls_remote_address.equalsIgnoreCase(ls_localaddress));
} catch (Exception e) {
}
return false;
}
This will work whether you have VISTA and IPv4 and IPv6 enabled at the
same time.
This method also has the has the added advantage of not depending on
the request object (which can be tempered with) to obtain the local
address but instead use InetAddress.getLocalAddress() method.
Hope someone finds this useful
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---