Level15
Level15

Reputation: 75

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.

Upvotes: 3

Views: 27728

Answers (5)

Sakthi
Sakthi

Reputation: 11

You cannot get computer name using javascript. Javascript does not have access to computer name as a security reason

Upvotes: 1

Dominik Pfefferle
Dominik Pfefferle

Reputation: 36

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)

Upvotes: 2

Prim
Prim

Reputation: 2968

You can use InetAddress.getLocalHost().getHostName() which doesn't resolve hostname from DNS according to the documentation

Upvotes: -1

Sheetal Mohan Sharma
Sheetal Mohan Sharma

Reputation: 2924

Tried servlet?

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 

    final String hostname = request.getRemoteHost(); // hostname
    System.out.println("hostname"+hostname);

    String remoteMachineName = null;
    String remoteMachineAddress = request.getRemoteAddr();
    System.out.println("remoteMachineAddress: " + remoteMachineAddress);
    try {
        InetAddress inetAddress = InetAddress.getByName(remoteMachineAddress);

        remoteMachineName = inetAddress.getHostName();
        System.out.println("remoteMachineName: " + remoteMachineName);
       } 
    } catch (UnknownHostException e) {

        }

    System.out.println("remoteMachineName: " + remoteMachineName);
}    

Upvotes: 0

if you have the ip add the you can do:

InetAddress inetAddress =InetAddress.getByName("127.64.84.2");//get the host Inet using ip
System.out.println ("Host Name: "+ inetAddress.getHostName());//display the host

Upvotes: 0

Related Questions