Search code examples
javascriptjavafirefoxappletclient

Get client computer name in web application


I have a Java web application that needs to know the computer name of clients connecting to it. My first idea was to get it via JavaScript and fill a hidden form field with it, but after some digging, it appears JS cannot access that information.

Then I tried using an applet and accessing the applet methods via JavaScript. This seems to work on Firefox, but newer Chrome versions don't run applets.

Then I considered switching to a Java Webstart application, which, as far as I know, should work under Chrome, but in this scenario, since the Webstart application runs outside the browser,JavaScript cannot access its methods.

Then I tried saving the hostname in the environment TEMP directory, which works, in Firefox + Linux + Java7, but not in Firefox + Windows + Java8: the applet just doesn't run, and, in addition, I haven't found a way to access the defined TEMP directory and read the file in JavaScript.

At this point I'm out of ideas and would love to have some input from you guys. Any hints on how to achieve this? Did I miss any obvious solution?

Please notice I need the computer defined hostname, not what the computer's IP resolves to via DNS.

Thanks.


Solution

  • Your Javawebstartet Application could Host Websocket listener. So you could access this application via Websocket from javascript. Works only with http, not https

    On JavaSide use the websocket implementation of http://java-websocket.org/

    In Javascript I use https://code.google.com/p/jquery-websocket/ You can find examples there too. The websocket communication is async. Create the WS with the callback method for the response

    var ws = $.websocket("ws://127.0.0.1:" + lport + "/", {
                events: {
                    say: function (e) {
                        //showMsg(e.data);
    
                    }
                }
            });
    

    and call the server with

    ws.send(0, jsonData)