admin管理员组文章数量:1291105
I have a Java web application that needs to know the puter 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 puter defined hostname, not what the puter's IP resolves to via DNS.
Thanks.
I have a Java web application that needs to know the puter 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 puter defined hostname, not what the puter's IP resolves to via DNS.
Thanks.
Share Improve this question asked Nov 27, 2015 at 15:43 Level15Level15 751 gold badge1 silver badge7 bronze badges5 Answers
Reset to default 2Your 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/
In Javascript I use https://code.google./p/jquery-websocket/ You can find examples there too. The websocket munication 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)
You cannot get puter name using javascript. Javascript does not have access to puter name as a security reason
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
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);
}
You can use InetAddress.getLocalHost().getHostName()
which doesn't resolve hostname from DNS according to the documentation
本文标签: javascriptGet client computer name in web applicationStack Overflow
版权声明:本文标题:javascript - Get client computer name in web application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741441901a2378970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论