Detect client side IP

250 views
Skip to first unread message

Javid

unread,
Jun 22, 2006, 12:55:43 PM6/22/06
to Google Web Toolkit
Hi,
I am trying to detect the client side IP, so I wrote a native JSNI
method to detect the client IP

private native String getIP() /*-{
var ip = @java.net.InetAddress::getLocalHost() ();
var ipStr = ip.@java.net.InetAddress::getHostAddress() ();
return ipStr;
}-*/;

It works fine on the hosted mode but when I try to compile I get the
following error

Output will be written into C:\Java
development\SAGEUI\www\evl.sage.SageUI
Analyzing permutation #1
Errors in file:/C:/Java
development/SAGEUI/src/evl/sage/client/SageDisplay
.java
[ERROR] Line 300: Unresolvable native reference to type
'java.net.Inet
Address'
[ERROR] Line 300: Unresolvable native reference to type
'java.net.Inet
Address'
[ERROR] Unexpected internal compiler error
[ERROR] Build failed

Can someone help me with this issue,

Thanks,
Javid

Sriram Narayanan

unread,
Jun 22, 2006, 1:31:38 PM6/22/06
to Google-We...@googlegroups.com

When you compile GWT code, the service code resides in the server and
the UI code gets converted in Javascript code. Since you can't access
java.net code in Javascript, the compiler doesn't compile such code
either.

Try the following:

EntryPoint.jsp
<!-- Use a jsp instead of an HTML, you need to get data from the
server to the browser -->
<script language="javascript">
<%
String ip_address = getHostAddress(); // that's code to get the
client's IP address
%>

var _ipAddress = <%= ip_address %>;
// var _ipAddress = "10.0.0.1";
</script>

<!-- remainder of your jsp page -->

Now, in the Java code, use something like:

private static native JavaScriptObject getIP() /*-{
return $wnd._ipAddress;
}-*/;

private void someFunction(){
String client_ip = getIP().toString();
}

What I've done here is :
1. Assemble a client specific page containing the client's IP address.
2. Send this to the client.
3. Include within this page the gwt bootstrap code.
4. Write some GWT code that makes a Javascript call. The call seeks
the variable _ipAddress in the Window object.

I've tried the above and it works for me.

-- Sriram

Javid Alimohideen

unread,
Jun 22, 2006, 4:04:15 PM6/22/06
to Google-We...@googlegroups.com
Hi Sriram,
The code you had suggested gets the server's ip address  but what I am trying to do is get the client side browser ip address and send it to the server. Do you know how I could do this using GWT?

Thanks,
Javid

Sriram Narayanan

unread,
Jun 22, 2006, 4:20:13 PM6/22/06
to Google-We...@googlegroups.com
On 6/23/06, Javid Alimohideen <javid.al...@gmail.com> wrote:
> Hi Sriram,
> The code you had suggested gets the server's ip address but what I am
> trying to do is get the client side browser ip address and send it to the
> server. Do you know how I could do this using GWT?
>

Er.. I'd told you about getHostAddress() intending it to be code
that'll return to you the remote Address.

Replace String ip_address = getHostAddress();

with:
String ip_address = request.getRemoteAddr();


> Thanks,
> Javid

-- Sriram

Javid Alimohideen

unread,
Jun 22, 2006, 4:53:51 PM6/22/06
to Google-We...@googlegroups.com
Thanks it works...

On 6/22/06, Sriram Narayanan <srir...@gmail.com> wrote:

mP

unread,
Jun 23, 2006, 6:49:09 AM6/23/06
to Google Web Toolkit
I really wish people would read the doco twice before posting here. The
doco quite clearly describes what restrictions and which classes are
available on the client with translated java -> javascript.

Sriram Narayanan

unread,
Jun 23, 2006, 8:54:49 AM6/23/06
to Google-We...@googlegroups.com

True.

However, thinking in the GWT way of web app development requires some time.

-- Sriram

Reply all
Reply to author
Forward
0 new messages